If you are still using the ASP.NET SOAP web service, then the easiest way that suits your requirements IMO is to use ASP.NET Forms authentication with a membership database. If you are starting a new one, I would recommend going with WCF - if you cannot / will not do this, this post refers to the "classic" ASP.NET SOAP web services.
To add forms authentication to a web service:
Set it up just like you would on any other website, but set it up for everyone to access:
<authorization> <allow users="*"/> </authorization>
Implement login / logout methods and issue an authentication ticket in the Login method. Further requests to the web service can then use the issued authentication ticket.
All other web methods you want to protect can then beautify with
[PrincipalPermission (SecurityAction.Demand, Authenticated = true)]
These methods now raise a security exception if the client is not authenticated.
Example for a protected method:
[PrincipalPermission(SecurityAction.Demand, Authenticated = true)] [WebMethod(Description = "Your protected method")] public string Foo() { return "bar"; }
Example for login method:
[WebMethod(Description = "Login to start a session")] public bool Login(string userName, string password) { if (!Membership.Provider.ValidateUser(userName, password)) return false; FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, userName, DateTime.Now, DateTime.Now.AddMinutes(500), false, FormsAuthentication.FormsCookiePath);
Brokenglass
source share