登录Asp.Net Identity项目

时间:2015-06-14 19:18:42

标签: c# asp.net-mvc asp.net-identity-2

我正在做一个基于Asp.Net的Web应用程序,以及基于Identity Framework的登录系统。我遇到的问题有点奇怪。

我运行的应用程序将我带到登录页面,我登录(并重定向到主页)然后如果我注销并尝试再次登录它会一直给我登录页面而不是主页,但如果我这样做second在异步调用中使用BreakPoint登录,然后逐步完成登录,并将我重定向到主页。

代码是:

[HttpPost]
        [AllowAnonymous]
        public async Task<ActionResult> LogIn(LogInModel model)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
         //BreakPoint
         var user = await userManager.FindAsync(model.Email, model.Password);

            if (user != null)
            {
                var identity = await userManager.CreateIdentityAsync(
                    user, DefaultAuthenticationTypes.ApplicationCookie);

                SignIn(user);

                return Redirect(GetRedirectUrl(model.ReturnUrl));
            }

            // user authN failed
            ModelState.AddModelError("", "Invalid email or password");
            return View();
        }

     private string GetRedirectUrl(string returnUrl)
    {
        if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
        {
            return Url.Action("index", "home");
        }

        return returnUrl;
    }

    private async Task SignIn(IdentityUser user)
    {
        var identity = await userManager.CreateIdentityAsync(
            user, DefaultAuthenticationTypes.ApplicationCookie);
        GetAuthenticationManager().SignIn(identity);
    }

    private IAuthenticationManager GetAuthenticationManager()
    {
        var ctx = Request.GetOwinContext();
        return ctx.Authentication;
    }

    public ActionResult LogOut()
    {
        var ctx = Request.GetOwinContext();
        var authManager = ctx.Authentication;
        Session.Abandon();

        authManager.SignOut("ApplicationCookie");
        return RedirectToAction("LogIn", "Login");
    }

0 个答案:

没有答案