【发布时间】: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