简体   繁体   English

Asp.net 核心身份登录成功重定向回登录页面

[英]Asp.net core Identity successful login redirecting back to login page

I have a problem where the asp.net identity framework is redirecting the user back to the login page after they have logged in successfully.我遇到一个问题,即 asp.net 身份框架在用户成功登录后将用户重定向回登录页面。

This is using the standard Asp.net Core Identity.这是使用标准的 Asp.net Core Identity。 It is the version 2.1.1.它是版本 2.1.1。 The scaffolding that generates the razor pages.生成 razor 页的脚手架。 Not sure if that is significant.不确定这是否重要。

I know the user is successfully logging in because I get the log message我知道用户已成功登录,因为我收到了日志消息

...Areas.Identity.Pages.Account.LoginModel: Information: User logged in. ...Areas.Identity.Pages.Account.LoginModel:信息:用户已登录。

But then it redirects straight back to the login page.但随后它直接重定向回登录页面。

If I use fiddler I can see that there is a cookie on the request so it all looks good from that perspective.如果我使用 fiddler,我可以看到请求中有一个 cookie,所以从这个角度来看一切看起来都不错。

.AspNetCore.Identity.Application=CfDJ8KJxkuir9ZJIjFLCU2bzm9n6X...

So I guess the middleware that is handling the authentication but not accepting the cookie?所以我猜是处理身份验证但不接受 cookie 的中间件?

If I could see what the actual middleware for the auth was doing I might have an idea but I can't find it.如果我能看到授权的实际中间件在做什么,我可能会有一个想法,但我找不到它。

Any help appreciated任何帮助表示赞赏

In order to get the ASP.NET Core pipeline to recognise that a user is signed in, a call to UseAuthentication is required in the Configure method of your Startup class, like so:为了让 ASP.NET Core 管道识别用户已登录,需要在Startup类的Configure方法中调用UseAuthentication ,如下所示:

app.UseAuthentication();
app.UseMvc(); // Order here is important (explained below).

Using the Cookies authentication scheme, the use of UseAuthentication loosely performs the following:使用 Cookies 身份验证方案, UseAuthentication的使用松散地执行以下操作:

  • Reads the content of the .AspNetCore.Identity.Application cookie from the request, which represents the identity of the user making the request.从请求中读取.AspNetCore.Identity.Application cookie 的内容,它表示发出请求的用户的身份。
  • Populates the User property of HttpContext with a ClaimsPrincipal that represents said user. User代表所述用户的ClaimsPrincipal填充HttpContextUser属性。

This is a simplified explanation of what happens, but it highlights the important job that the authentication middleware performs.这是对所发生情况的简化解释,但它突出了身份验证中间件执行的重要工作。 Without the authentication middleware, the .AspNetCore.Identity.Application will not be used for authenticating the user and therefore the user will not be authenticated.如果没有身份验证中间件, .AspNetCore.Identity.Application将不会用于对用户进行身份验证,因此不会对用户进行身份验证。 In your case, although the user has signed in (ie the cookie is being set), the pipeline middleware (eg MVC) does not see this user (ie the cookie is not being read) and so sees an unauthenticated request and redirects again for login.在您的情况下,尽管用户已登录(即正在设置 cookie),但管道中间件(例如 MVC)看不到该用户(即未读取 cookie),因此会看到未经身份验证的请求并再次重定向登录。

Given that the authentication middleware reads the cookie and subsequently populates the ClaimsPrincipal , it should be clear that the UseAuthentication call must also be before the UseMvc call in order for this to occur in the correct order.鉴于身份验证中间件读取 cookie 并随后填充ClaimsPrincipal ,应该清楚UseAuthentication调用也必须UseMvc调用之前,以便以正确的顺序发生。 Otherwise, the MVC middleware runs before the Authentication middleware and will not be working with a populated ClaimsPrincipal .否则,MVC 中间件在 Authentication 中间件之前运行,并且不会与填充的ClaimsPrincipal一起工作。

Why is it failing to login if you don't add the middleware that handles the login?!?为什么不添加处理登录的中间件会登录失败?!?

The middleware doesn't handle the login - it handles the authentication process.中间件不处理登录 - 它处理身份验证过程。 The user has logged in, which is confirmed by the presence of the .AspNetCore.Identity.Application cookie.用户登录,这由.AspNetCore.Identity.Application cookie 的存在确认。 What is failing here is the reading of said cookie.这里失败的是读取所述cookie。

Note that the order is IMPORTANT.请注意,该顺序很重要。 I had these reversed and experienced the exact same problem the original poster experienced.我将这些颠倒过来并遇到了与原始海报所遇到的完全相同的问题。 Hours wasted before I figured this out...在我弄明白之前浪费了几个小时......

    app.UseAuthentication();
    app.UseAuthorization();

In addition to @Kirk Larkin answer if you use .net core 3 (in this time is preview version)除了@Kirk Larkin 回答,如果你使用 .net core 3(这次是预览版)

put your "app.UseEndpoints" in startup.cs to end of the code block.将您的“app.UseEndpoints”放在 startup.cs 到代码块的末尾。

for example it should be in this order例如它应该按这个顺序

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

app.UseAuthentication(); app.UseAuthentication(); app.UseMvc(); app.UseMvc();

This was already in my code.However I was facing same issue.这已经在我的代码中了。但是我遇到了同样的问题。 But the problem was with the Chrome browser on my case.It was working with other browser like mozilla.但问题出在我的 Chrome 浏览器上。它可以与其他浏览器(如 mozilla)一起使用。 It starts to work on Chrome too, after i cleared all the cookies and cashes.在我清除了所有 cookie 和现金后,它也开始在 Chrome 上工作。

I had to do a couple of these solutions.我不得不做几个这样的解决方案。

First, I had app.UseAuthentication();首先,我有 app.UseAuthentication(); and app.UseAuthorization();和 app.UseAuthorization(); in the incorrect order.以错误的顺序。

Second, After making the adjustment to startup.cs I was still experiencing the same problem.其次,在对startup.cs进行调整后,我仍然遇到同样的问题。 Doing a hard refresh a few times fixed it (can also just clear the browser cache).做几次硬刷新修复它(也可以只清除浏览器缓存)。

I tried many suggestions and finally what worked for me was: First, in your ConfigureServices class within "Startup.cs" add this我尝试了很多建议,最后对我有用的是:首先,在“Startup.cs”中的 ConfigureServices 类中添加这个

services.AddMvc().AddMvcOptions(Mvcoption => {
            Mvcoption.EnableEndpointRouting = false;
        });

The above code should come after this:上面的代码应该在此之后:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = "/Home/Login";


                });

Just in case you don't have the above two codes, try including them in your mvc application.以防万一您没有上述两个代码,请尝试将它们包含在您的 mvc 应用程序中。 Then you can go ahead and add these in your Configure method.然后您可以继续将这些添加到您的 Configure 方法中。

app.UseAuthentication();
        app.UseMvc();

Hope it works now.希望它现在有效。

Happy coding!快乐编码!

Another gotcha that I fought with for a number of hours is that I had the following code in my Program file.另一个让我纠结了好几个小时的陷阱是我的程序文件中有以下代码。

builder.Services.AddAuthorization(options =>
{
    // By default, all incoming requests will be authorized according to the 
default policy
    options.FallbackPolicy = options.DefaultPolicy;
});

This had the impact of causing a recursive call to the ReturnUrl and ended up with the following error.这产生了对 ReturnUrl 的递归调用的影响,并以以下错误结束。 Simply removing this code block rectified the issue for me.简单地删除这个代码块就为我解决了这个问题。

递归返回网址

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 Identity Core 和 Asp.net Core 3.1 的多重登录页面 - Multiple Login Page with Identity Core and Asp.net Core 3.1 asp.net重定向到登录页面 - asp.net redirecting to login page 在 asp.net 核心成功登录后,User.Identity.IsAuthenticated 在非身份验证方法中为 false - User.Identity.IsAuthenticated is false in a non-auth methods after successful login in asp.net core 登录成功后传递用户详细信息来自身份asp.net core 3.1 - Pass user detail after Login Is successful From identity asp.net core 3.1 成功注册/登录后不显示注销按钮,ASP.NET Core 标识 - LOGOUT button not showing after successful REGISTER/LOGIN, ASP.NET Core Identity 使用 ASP.Net Core 3.1 进行身份验证 - 在未通过身份验证时,应用程序不会像在开发中那样重定向到生产环境中的登录 - Identity with ASP.Net Core 3.1 - on not authenticated, the app is not redirecting to Login in production like it does in dev 成功登录尝试后,系统再次重定向到 ASP.NET CORE 中的登录页面 - After Successfull Login Attempt the System is redirecting to Login Page Again in ASP.NET CORE 成功登录后,asp.net core 2.2重定向到登录 - asp.net core 2.2 redirects to login after successful sign in 带有 ASP.NET 内核 (MVC) 的登录页面 - Login Page with ASP.NET Core (MVC) 如何强制启动 Asp.Net Core 3.0 应用程序以使用身份登录页面 - How to force start Asp.Net Core 3.0 application to use Identity Login Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM