【问题标题】:Identity Server 4 (2.0) not reading Asp.Net Core Identity cookiesIdentity Server 4 (2.0) 不读取 Asp.Net Core Identity cookie
【发布时间】:2018-06-03 21:17:51
【问题描述】:

我正在尝试将Asp .Net Identity CoreIdentity Server 4 一起使用。我可以在日志 (Ids) 中看到用户已正确登录。

信息:Xena.IdentityServer.Controllers.AccountController[0] 用户已登录。

然后我的登录控制器将用户发送到我的管理控制器。

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

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

用户永远不会到达,因为登录由于某种原因而丢失。

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

我认为部分线索是这一行Authorization failed for user: (null). 我可以看到cookie 在浏览器中。它只是没有被阅读。

据我了解,Identity server 4 有自己的 cookie,Asp .Net core Identity 也有,它们需要读取相同的 cookie。我试过关注Using ASP.NET Core Identity,但没有帮助。

在身份服务器项目中启动

 //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>();

添加以下内容确实解决了我的管理/索引问题。但是它不起作用,因为那时打开的 Id 连接登录将不起作用,因为它使用 Identity Server 中的内部控制器进行身份验证,我不能/不想超载。我需要弄清楚如何让 Identity Server 4 使用由 Aspnet 身份设置的 cookie,反之亦然。

//[Authorize(AuthenticationSchemes = "Identity.Application")]

这个解决方案来自我在Stack 上提出的一个问题/p>

【问题讨论】:

  • 这似乎是一个奇怪的设置。我认为您的问题是,当身份验证后从身份服务器重定向到您的控制器时,ASP 身份正在接管并拒绝持有者令牌。如果您使用 Identity Server 作为身份提供者,为什么要以这种方式使用 ASP Identity?更好的解决方案是查看 Identity Server 4 文档,并查看如何将 Identity Server 的用户存储与 ASP Identity 结合起来,并将其仅用于用户管理。然后,您可以在客户端应用程序中使用 openid connect 并将 Idsvr4 设置为 Provider。
  • 不确定我理解该语句的大部分内容想要再试一次吗?我一直在关注身份服务器教程,这就是我走到这一步的方式。我们有一个指向 ID 的网络应用程序。只有 Id 能够在用户位于其数据库中时对其进行编辑。我需要能够允许 web api 和 3rd 方插件对 id 进行身份验证,并允许用户更新他们的用户数据。如果我做错了什么,请详细说明。
  • 这是我一直关注的示例之一的链接github.com/IdentityServer/IdentityServer4.Samples/blob/release/…
  • 抱歉,这个很难说清楚,我假设上面的启动是客户端应用程序的配置。而且您在中间件中设置了 ASP 身份,但为什么呢?当身份服务器提供身份验证时。您在客户端应用程序中需要它做什么?
  • 我很想创建一个虚拟项目,看看是否可行。这是 IDS 1.x 到 2.0 的升级形式,令人沮丧。

标签: c# cookies asp.net-core-mvc asp.net-identity identityserver4


【解决方案1】:

今天早上我终于找到了问题所在。部分问题是由于我有一个使用 IdentityConstant cookie 的自定义登录管理器。

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

DefaultAuthenticateScheme 和 DefaultChallengeScheme 都需要设置。然后一切正常。

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 2015-08-09
    • 2016-11-18
    • 2020-06-08
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 2016-10-14
    相关资源
    最近更新 更多