gpt4 book ai didi

c# - 如何获取 OpenIdConnectOptions StateDataFormat

转载 作者:行者123 更新时间:2023-12-03 17:29:21 26 4
gpt4 key购买 nike

我有一个使用 IdentityServer4 的自定义 Open Id Connect 服务器.此 SSO 服务器具有用于非标准 Open Id Connect 操作的自定义端点。

为了使用此操作,我需要在客户端应用程序的请求中创建一个状态参数。客户端应用程序是带有 blazor 服务器端的 dotnet core 3.0 预览应用程序。

我正在使用以下内容注册 SSO 提供程序:

       services.AddAuthentication(options => {
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "oidc";
})
.AddCookie()
.AddOpenIdConnect("oidc", options => {
options.ClientId = oidcClientId;
options.ClientSecret = "secret";
options.Authority = "https://test.org";
options.ResponseType = "code id_token";
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("extra");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.RequireHttpsMetadata = false;
});

基本的登录/注销流程按预期工作,没有任何问题。

我为使用自定义 SSO 功能而生成的自定义链接如下所示( Razor ):
            <a href=@($"https://test.org/custom/change-organization/{organization.OrganizationCode}?client_id=local-data-manager&redirect_uri=http://localhost:5000/signin-oidc&response_type=code id_token&scope=openid custom profile&nonce={GenerateNonce()}&state={GenerateState()}")>@organization.Name</a>

使用定义的方法:
  private string GenerateNonce()
{
string nonce = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString() + Guid.NewGuid().ToString()));
return DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture) + "." + nonce;
}

private string GenerateState()
{
var state = GenerateNonce();
AuthenticationProperties authProperties = new AuthenticationProperties
(
new Dictionary<string, string>
{
{ OpenIdConnectDefaults.UserstatePropertiesKey, state },
}
);

authProperties.RedirectUri = "http://localhost:5000";

//This StateDataFormat does not use the correct DataProtectionProvider
return openIdOptions.CurrentValue.StateDataFormat.Protect(authProperties);
}

在哪里 openIdOptions是依赖注入(inject)。如 @inject IOptionsMonitor<OpenIdConnectOptions> openIdOptions
当我单击标签并触发自定义流程时,一切都在 SSO 端按预期工作,但是当消息返回时,我收到以下错误:
An unhandled exception has occurred while executing the request.                                                                                                                                             
System.Exception: An error was encountered while handling the remote login.
System.Exception: Unable to unprotect the message.State.

这是抛出 from the OpenIdHandler调用 Unprotect我认为是相同的 StateDataFormat之前注入(inject)的保护器:
Options.StateDataFormat.Unprotect(message.State);
从详细日志记录中,我在调用 GenerateState() 时看到以下内容:
trce: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector[31]
Performing protect operation to key {cdf7e79b-8d1a-4e7e-a093-fea402dbba8c} with purposes ('/app/DataManager', 'Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler', '', 'v1').

但是当请求回来时,我得到了 unprotect 日志:
trce: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector[5]
Performing unprotect operation to key {cdf7e79b-8d1a-4e7e-a093-fea402dbba8c} with purposes ('/app/DataManager', 'Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler', 'oidc', 'v1').

主要区别在于 DataProtector 的“用途”不同(一个具有 '' 和一个具有 'oidc')。 Which according to the documentation means they are being encrypted and decrypted differently .

我希望数据保护器是相同的,因为它们都引用相同的 OpenIdConnectOptions配置的。

为什么是那两个 DataProtector是提供不同的目的吗?它们是不同的对象吗?我的配置中是否缺少某些内容?如何从内部框架和我注入(inject)的内容中获得相同的提供者?

最佳答案

我有同样的问题并找到了答案,我们需要通过方案名称来获取正确的实例:

return _openIdOptions.Get("oidc").CurrentValue.StateDataFormat.Protect(authProperties);

关于c# - 如何获取 OpenIdConnectOptions StateDataFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57581119/

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