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

时间:2022-10-20 08:25:18

I am working on MVC5 Project, it works fine on my system but it's behaving strange after deployment on server. I used OWIN for authentication, it works fine for first login, but after few seconds if I refresh the page, it redirects me back to the login page (This on happens on deployed server).

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

My Code:

     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);}

I used [Authorize] on my controller.

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

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

Here's my login code:

这是我的登录代码:

    [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 个解决方案

#1


0  

At a glance, its either that the cookie is not being set correctly or at all. Or that the ModelState for some reason is not valid and your redirect is never hit.

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

Check out the link :-

查看链接: -

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

It should help with your configuration of OWIN middleware.

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

#2


0  

I added machineKey and sessionState in my Web.config file and that resolved the issue.

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

#3


0  

I had a similar issue which was caused by not using SSL. Choose the project in Solution Explorer, hit F4 and change to use SSL. Login requires SSL, a setting you can change but if set wrong it causes this confusing loop.

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

#1


0  

At a glance, its either that the cookie is not being set correctly or at all. Or that the ModelState for some reason is not valid and your redirect is never hit.

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

Check out the link :-

查看链接: -

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

It should help with your configuration of OWIN middleware.

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

#2


0  

I added machineKey and sessionState in my Web.config file and that resolved the issue.

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

#3


0  

I had a similar issue which was caused by not using SSL. Choose the project in Solution Explorer, hit F4 and change to use SSL. Login requires SSL, a setting you can change but if set wrong it causes this confusing loop.

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