I want the benefits of form authentication in ASP.NET. I want it to persist the authorization for me and such, but there's one thing different about my situation; I want to authenticate against a simple web service (specifically provided by the client).
I have my code in place to look at the web place and see if they should be authorized, but how do I set the cookie[?] or authorization flag in ASP.NET that they know the current user is authorized.
Basically...
if (HttpContext.Current.User.Identity.IsAuthenticated)
// we're all good
//Other wise...
bool success = CheckClientsWebService(string username, string password);
if (success)
// Somehow tell .NET that they're authorized
*Note: This is a fairly simple service that does not deal with groups or roles. Simply checking if a user is okay to view the site.
In forms authentication isn't the proof of who you are in th forms authentication cookie.? With that in mind couldn't you create the ticket in a custom login form without having to create a custom provider? I would definitely think you could. Do a quick test and create a forms authentication ticket and see if the out of the box membership provider considers the user authenticated.
I was curious-- so here is some code..
Model
Controller
Index.cshtml
Secure.cshtml
I may be over simplifying this, but the way I read this is the following:
If the above is correct, you do not need a membership provider. The [Authorize] attribute simply looks that the forms authentication cookie to see if it has been set and is valid for the current lifetime of the cookie. This authentication cookie stores the username of the user and the expiration time of the cookie (and other stuff, but not important here).
Given that, you only need to set your web.config configuration element and have a method to set the authentication cookie.
Web.Config
Logon URL GET action
Logon URL POST action
Once you have called the
.SetAuthCookie()
function, the user will now have an authentication ticket and calls toHttpContext.User.Identity.IsAuthenticated
will be true as long as the cookie has not expired and you can get the user name fromHttpContext.User.Identity.Name
As Wiktor commented, implement your own MembershipProvider. Just implement the methods you need, leave the rest throwing a
NotImplementedException
.In your case, it looks like all you need to implement is
public bool ValidateUser(string username, string password)
- the implementation of which just needs to call through to your webservice.Then you can use all the standard built-in authentication and authorization stuff.