即使登录后,MVC5身份验证也会重定向到登录页面

时间:2015-07-13 19:32:28

标签: asp.net login asp.net-mvc-5 owin

我正在研究MVC5项目,它在我的系统上工作正常,但在服务器上部署后表现得很奇怪。我使用OWIN进行身份验证,它在首次登录时工作正常,但是如果我刷新页面几秒钟后,它会将我重定向回登录页面(这在部署的服务器上发生)。

我的代码:

     public void ConfigureAuth(IAppBuilder app)
     {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);}

我在控制器上使用了[授权]。

[Authorize]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

这是我的登录代码:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginUserModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.userName, model.password);
            if (user != null)
            {
                return RedirectToLocal(returnUrl);
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

乍一看,要么没有正确设置cookie,要么根本没有设置。或者由于某种原因,ModelState无效,您的重定向永远不会被命中。

查看链接: -

http://coding.abel.nu/2014/06/understanding-the-owin-external-authentication-pipeline/

它应该有助于您配置OWIN中间件。

答案 1 :(得分:0)

我在我的Web.config文件中添加了machineKey和sessionState,解决了这个问题。

答案 2 :(得分:0)

我有一个类似的问题是由于没有使用SSL造成的。在解决方案资源管理器中选择项目,单击F4并更改为使用SSL。登录需要SSL,您可以更改设置,但如果设置错误,则会导致此混乱循环。