简体   繁体   English

Identity Server 4(2.0)不读取Asp.Net核心标识cookie

[英]Identity Server 4 (2.0) not reading Asp.Net Core Identity cookies

I am trying to use Asp .Net Identity Core with Identity Server 4 . 我正在尝试将Asp .Net Identity CoreIdentity Server 4一起使用 I can see in the logs (Ids) that the user is logged in properly. 我可以在日志(Ids)中看到用户已正确登录。

info: Xena.IdentityServer.Controllers.AccountController[0] User logged in. info:Xena.IdentityServer.Controllers.AccountController [0]用户登录。

My login controller then sends the user over to my Manage controller. 我的登录控制器然后将用户发送到我的管理控制器。

[Route("[controller]/[action]")]
[Authorize]
//[Authorize(AuthenticationSchemes = "Identity.Application")]
public class ManageController : Controller
{

    [HttpGet]
    public async Task<IActionResult> Index(ManageMessageId? message = null)
    {
     ..... 
    }
 }

The User never arrives as the login is then lost for some reason. 用户永远不会到达,因为登录因某种原因而丢失。

info: Microsoft.AspNetCore.Mvc.RedirectToActionResult[2]
      Executing RedirectResult, redirecting to /Manage/Index.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Xena.IdentityServer.Controllers.AccountController.Login (Xena.IdentityServer) in 3493.6102ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 3515.9158ms 302
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request starting HTTP/1.1 GET http://localhost:5000/Manage/Index
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed for user: (null).
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
      Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[2]
      Executing ChallengeResult with authentication schemes ().
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[12]
      AuthenticationScheme: Bearer was challenged.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Xena.IdentityServer.Controllers.ManageController.Index (Xena.IdentityServer) in 46.2285ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 58.6793ms 401

I think part of the clue is this line Authorization failed for user: (null). 我认为部分线索是这一行Authorization failed for user: (null). I can see that the cookie is in the browser. 我可以看到cookie在浏览器中。 Its just not being read. 它只是没有被阅读。

From what I understand Identity server 4 has its own cookies and Asp .Net core Identity has its and they need to be reading the same cookie. 据我所知,身份服务器4有自己的cookie和Asp .Net核心身份有它,他们需要读取相同的cookie。 I have tried following Using ASP.NET Core Identity but it hasn't helped. 我尝试过使用ASP.NET核心标识,但它没有帮助。

Startup in the Identity server project 在Identity Server项目中启动

 //Adds Asp.net identity 
        services.AddDbContext<ApplicationDbContext>(builder =>
            builder.UseSqlServer(Configuration.GetConnectionString("XenaIdentityConnection")));

        // Configuer Asp.net Identity
        services.AddIdentity<ApplicationUser, IdentityRole<long>>(config =>
            {
                config.Password.RequireDigit = true;
                config.Password.RequireLowercase = true;
                config.Password.RequireNonAlphanumeric = false;
                config.Password.RequireUppercase = true;
                config.Password.RequiredLength = 8;
                config.User.RequireUniqueEmail = true;
                config.SignIn.RequireConfirmedEmail = true;
                config.SignIn.RequireConfirmedPhoneNumber = false;
            })
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddSignInManager<ApplicationSignInManager>()    // Adds custom SignIn manager.
            .AddDefaultTokenProviders();


   //https://identityserver4.readthedocs.io/en/release/topics/signin.html
        services.AddAuthentication(options =>
            {
                options.DefaultScheme = IdentityConstants.ApplicationScheme;
            })
            .AddGoogle("Google", options =>
            {
                options.AccessType = "offline";
                options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                options.ClientId = "xxxxx-jvu30c2n19thoqimd97b4jk1r2poh17p.apps.googleusercontent.com";
                options.ClientSecret = "g29nXgVoFZBIBNS-hJJxPWXW";
            }).AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, "OpenID Connect", options =>
            {
                options.SaveTokens = true;
                options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                options.SignOutScheme = IdentityServerConstants.SignoutScheme;
                options.RequireHttpsMetadata = settingsSetup.RequireHttpsMetadata;
                options.Authority = settingsSetup.Authority;
                options.ClientId = "testclient";
                options.Scope.Add("testapi");
                options.ResponseType = OpenIdConnectResponseType.IdTokenToken;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = "name",
                    RoleClaimType = "role"
                };
            });

   services.AddIdentityServer()
            .AddSigningCredential(LoadCertificate())
            .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = builder =>
                    builder.UseSqlServer(Configuration.GetConnectionString("XenaIdentityConnection"),
                        sql => sql.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name));
            })
            .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = builder =>
                    builder.UseSqlServer(Configuration.GetConnectionString("XenaIdentityConnection"),
                        sql => sql.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name));

            })
            .AddAspNetIdentity<ApplicationUser>()
            .AddProfileService<ProfileService>();

Adding the following does fix my issue with manage/index. 添加以下内容确实解决了我的manage / index问题。 However it doesn't work because then the open Id connect login wont work because that uses internal controllers within Identity Server for authentication that I cant / don't want to overload. 但它不起作用,因为那时打开的Id连接登录不会工作,因为它使用Identity Server中的内部控制器进行身份验证,我不想重载。 I need to figure out how to get Identity Server 4 to use the cookie set by Aspnet identity or visa versa. 我需要弄清楚如何让Identity Server 4使用Aspnet身份设置的cookie,反之亦然。

//[Authorize(AuthenticationSchemes = "Identity.Application")] // [授权(AuthenticationSchemes =“Identity.Application”)]

This solution came from a previous question i asked on Stack I have opened a new one because i am leaning towards this being a setup issue with Identity server rather than an issue with Identity cookies 这个解决方案来自我之前在Stack上提出的问题我已经开了一个新的问题,因为我倾向于这是身​​份服务器的设置问题,而不是身份cookie的问题

I finally figured out the problem this morning. 我终于在今天早上发现了问题。 Part of the problem was due to the fact that I have a custom signin manager which uses the IdentityConstant cookies. 部分问题是由于我有一个使用IdentityConstant cookie的自定义登录管理器。

services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
                    options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
                })

Both DefaultAuthenticateScheme and DefaultChallengeScheme need to be set. 需要设置DefaultAuthenticateScheme和DefaultChallengeScheme。 Then everything works as it should. 然后一切都按预期工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM