I can simplify this, but the way I read it is as follows:
- If the user is not authenticated, you have a form in which you collect the username / password
- The results of this form are submitted to the web service for authorization.
- If this authorization is successful, you need a way to tell the web application that they are logged in.
- If they are authenticated, do something.
If this is correct, you do not need a membership provider. The [Authorize] attribute simply looks like the forms authentication cookie determines whether it has been set and is valid for the current lifetime of the cookie. This authentication cookie saves the username and expiration time of the cookie (and other things, but not important here).
Given that you only need to set your web.config configuration element and set the method for setting the authentication cookie.
Web.config
<system.web> <authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="2880" /> </authentication> </system.web>
GET Login URL
public ActionResult Logon(){ //if the user is logged in, send the to the home page if(httpContext.User.Identity.IsAuthenticated_{ Return RedirectToAction("Index", "Home"); } Return this.View(new LoginViewModel()); }
POST action to login
[HttpPost] public ActionResult Logon(LoginViewModel model){ //Check for model errors if(!ModelState.IsValid()){ Return this.View(model); } //Validate against web service - return error if false if(!CheckClientsWebService(model.UserName, model.Password)){ ModelState.AddModelError("","The username or password is invalid"); Return this.View(model); } //Manually set the authentication cookie FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); //Send them on to the home page, they now have a authorization cookie Return RedirectToAction("Index", "Home"); }
After you .SetAuthCookie() function, the user will now have an authentication ticket, and the HttpContext.User.Identity.IsAuthenticated calls will be true if the cookie has not expired, and you can get the username from HttpContext.User.Identity.Name
Tommy
source share