【问题标题】:Fragment Urls with AspNetCore.Authentication.OpenIdConnect implicit flow带有 AspNetCore.Authentication.OpenIdConnect 隐式流的片段 URL
【发布时间】:2020-09-04 23:17:40
【问题描述】:

ASP.NET Core 3.1 在隐式流中使用 Microsoft.AspNetCore.Authentication.OpenIdConnect

我正在尝试执行 OpenIDConnect 隐式流。当我收到以下错误时,它似乎可以正常工作,直到回调:

异常:OpenIdConnectAuthenticationHandler:message.State 为空 或为空。

现在我怀疑这是因为我的代码无法获取状态和其他参数,因为它们位于哈希或 URL 片段的后面。在浏览器位置窗口中,我看到https://localhost:44300/signin-oidc#id_token=eyJ0&State=etc。等(注意哈希)。

我知道,在隐式流中,令牌被放置在哈希后面,并且可以像在 Angular 应用程序或诸如此类的东西中那样使用 javascript 读取。但我也认为 response_mode=form_post 会导致授权端点 POST 到回调。但是,在我的情况下,我似乎没有授权端点尊重这一点,或者出现了问题。这是我的 F12 日志:

Name                Url            
localhost           localhost (me)      GET 302 
auth?client_id=     authority           GET 302
auth?client_id=     web client auth     GET 200
signonCallback      authority           POST 302
signin-oidc         localhost(me)       GET 500

无奈之下,我在本地启动了 IdentityServer4 来测试隐式流,然后它会回发。不确定在现实世界中有什么不同。可能有很多不同,但我如何才能使用 ASP.NET Core 构造而不是求助于使用 javascript 从位置栏中提取哈希的某些页面?

代码:

services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
    options.AccessDeniedPath = new PathString("/Authorization/AccessDenied");
})   
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.ResponseType = OpenIdConnectResponseType.IdToken; //id_token
    options.ResponseMode = OpenIdConnectResponseMode.FormPost; //form_post

    options.Authority = Configuration["MyApp:Authentication:Authority"];
    options.GetClaimsFromUserInfoEndpoint = true;
    options.ClientId = Configuration["MyApp:Authentication:ClientId"];
    options.CallbackPath = 
        new PathString(Configuration["MyApp:Authentication:CallbackPath"]);
    options.SignedOutCallbackPath = 
        new PathString(Configuration["MyApp:Authentication:SignedOutCallbackPath"]);
    options.Scope.Clear();
    options.Scope.Add("openid");
    options.SaveTokens = true;
});

【问题讨论】:

    标签: asp.net-mvc asp.net-core openid-connect


    【解决方案1】:

    在隐式流程中,如果您希望 JavaScript 应用程序直接使用/访问令牌,请使用片段选项。当您希望将令牌发送回后端 (ASP.NET) 时,您可以使用 form_post 选项。

    如果可以的话,您应该尝试使用授权代码流,因为它更安全。

    如果您收到错误状态为 null 或为空,则表示初始身份验证请求缺少状态参数。 state 参数是一个安全参数,基本上客户端必须将其设置为随机值,当您获得令牌时,客户端应检查该状态是否相同。

    当您使用 AddOpenIdConnect 时,您希望它处理所有事情,包括创建对您的 openid-connect 服务器的初始请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 2020-05-17
      • 2021-12-09
      • 2019-01-14
      相关资源
      最近更新 更多