rustd / AspnetIdentitySample

476 stars 248 forks source link

No exception handling on claims controller Index... #41

Open mdelgadov opened 10 years ago

mdelgadov commented 10 years ago

In the ClaimsIdentityFactoryController, Index method, there is no exception handling for an invalid userid/password. I tried to use the same implementation than in the accounts controller, but the Return View(model) doesn't work because is not the Index view, but the Login view.

I tried the following code but can't be sure is ok... public class ClaimsIdentityFactoryController : Controller {

    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    // GET: ClaimsIdentityFactory
    public async Task<ActionResult> Index(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {

            var context = new DbContext();
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context))
            {
                ClaimsIdentityFactory = new oEazyClaimsIdentityFactory<ApplicationUser>()
            };

            // Create a User to SignIn
            var user = await userManager.FindAsync(model.Email, model.Password);
            if (user != null)
            {
                //SignIn the User by generating a ClaimsIdentity            
                var claimsIdentity =
                    await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

                // This claimsIdentity should have a claim called LastLoginTime
                var authManager = HttpContext.GetOwinContext().Authentication;
                authManager.SignIn(claimsIdentity);

                return RedirectToLocal(returnUrl);
            }

            ModelState.AddModelError("", "Invalid username or password.");
        }
        // If we got this far, something failed, redisplay form
        return View("_LoginPartial");
    }