gpt4 book ai didi

asp.net-core - 配置 ASP.Net Core 以使用 OIDC 针对 Thinktecture V2 进行身份验证

转载 作者:行者123 更新时间:2023-12-02 01:25:04 24 4
gpt4 key购买 nike

我正在尝试让 ASP.Net Core 使用 OpenID Connect 对 Thinktecture V2 进行身份验证(我们当前需要 WS-Trust,因此无法升级)。

我的配置如下

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

X509Store certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
certStore.Open(OpenFlags.ReadOnly);

var cert = certStore.Certificates.Find(X509FindType.FindByThumbprint, "CertThumbprint", false);

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
RequireHttpsMetadata = false,
ClientId = _config["OpenID:ClientId"],
ClientSecret = _config["OpenID:ClientSecret"],
Authority = _config["OpenID:Authority"],
ResponseType = OpenIdConnectResponseType.Code,
PostLogoutRedirectUri = _config["OpenID:PostLogoutRedirectUri"],
SignInScheme = "Cookies",
CallbackPath = "/signin-oidc",
TokenValidationParameters = new TokenValidationParameters()
{
IssuerSigningKey = new X509SecurityKey(cert[0]),
},
Configuration = new OpenIdConnectConfiguration
{

Issuer = "https://identityserver/IdentityServer/issue",
AuthorizationEndpoint = "https://identityserver/IdentityServer/issue/oidc/authorize",
TokenEndpoint = "https://identityserver/IdentityServer/issue/oidc/token",
UserInfoEndpoint = "https://identityserver/IdentityServer/issue/oidc/userinfo",

}
});

配置.json

"OpenID": {
"ClientId": "Test",
"ClientSecret": "{6DD502AB-2AB1-4028-BD4A-85C91790EC7B}",
"Authority": "https://identityserver/IdentityServer/issue/oidc",
"PostLogoutRedirectUri": "https://localhost:44353/" }

当我尝试进行身份验证时,出现以下异常:

HttpRequestException:响应状态代码不表示成功:400(错误请求)。

来自 thinktectureIdentityServer.svclog 的跟踪是

enter image description here

如果有人可以提供任何帮助,我们将不胜感激。

最佳答案

我通过处理 OnAuthorizationCodeReceivedEvent 并手动处理代码兑换解决了上述错误,其中我添加了基本授权 header 来授权客户端。

new OpenIdConnectOptions
{
...

Events = new OpenIdConnectEvents
{
OnAuthorizationCodeReceived = async context =>
{
context.HandleCodeRedemption();

var requestMessage = new HttpRequestMessage(HttpMethod.Post, context.Options.Configuration.TokenEndpoint);

requestMessage.Content = new FormUrlEncodedContent(context.TokenEndpointRequest.Parameters);

var authString = string.Format("{0}", Convert.ToBase64String(Encoding.ASCII.GetBytes(_config["OpenID:ClientId"] + ":" + _config["OpenID:ClientSecret"])));

requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authString);

var responseMessage = await context.Backchannel.SendAsync(requestMessage);

responseMessage.EnsureSuccessStatusCode();
var tokenResonse = await responseMessage.Content.ReadAsStringAsync();
var jsonTokenResponse = JObject.Parse(tokenResonse);
context.TokenEndpointResponse = new OpenIdConnectMessage(jsonTokenResponse);
}
}

...

});

为了进行最终调用来检索 UserInfo,我必须对身份服务器进行更改,以在响应中包含与 Id token 中的主题相匹配的主题。这涉及更新 UserInfoController 以在 Get 方法中添加声明。

关于asp.net-core - 配置 ASP.Net Core 以使用 OIDC 针对 Thinktecture V2 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39455776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com