gpt4 book ai didi

asp.net-web-api - 使用 AspNet.Security.OpenIdConnect 分离身份验证和资源服务器 - 受众?

转载 作者:行者123 更新时间:2023-12-03 13:33:49 25 4
gpt4 key购买 nike

AspNet.Security.OpenIdConnect.Server 上的示例在我看来,它既像身份验证服务器,又像资源服务器。我想把它们分开。我已经这样做了。

在身份验证服务器的 Startup.Config 中,我有以下设置:

app.UseOpenIdConnectServer(options => {

options.AllowInsecureHttp = true;
options.ApplicationCanDisplayErrors = true;
options.AuthenticationScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.Issuer = new System.Uri("http://localhost:61854"); // This auth server
options.Provider = new AuthorizationProvider();
options.TokenEndpointPath = new PathString("/token");
options.UseCertificate(new X509Certificate2(env.ApplicationBasePath + "\\mycertificate.pfx","mycertificate"));

});

我写了一个 AuthorizationProvider,但我认为它与我当前的问题无关(但可能相关)。在它的 GrantResourceOwnerCredentials 覆盖中,我硬编码了一个声明主体,以便它验证每个 token 请求:
public override Task GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsNotification context)
{
var identity = new ClaimsIdentity(OpenIdConnectDefaults.AuthenticationScheme);

identity.AddClaim(ClaimTypes.Name, "me");
identity.AddClaim(ClaimTypes.Email, "me@gmail.com");
var claimsPrincipal = new ClaimsPrincipal(identity);

context.Validated(claimsPrincipal);
return Task.FromResult<object>(null);
}

在资源服务器上,我的 Startup.config 中有以下内容:
app.UseWhen(context => context.Request.Path.StartsWithSegments(new PathString("/api")), branch =>
{
branch.UseOAuthBearerAuthentication(options => {
options.Audience = "http://localhost:54408"; // This resource server, I believe.
options.Authority = "http://localhost:61854"; // The auth server
options.AutomaticAuthentication = true;
});
});

在 Fiddler 上,我要一个 token ,我得到一个:
POST /token HTTP/1.1
Host: localhost:61854
Content-Type: application/x-www-form-urlencoded

username=admin&password=aaa000&grant_type=password

所以现在我使用该访问 token 从资源服务器访问 protected 资源:
GET /api/values HTTP/1.1
Host: localhost:54408
Content-Type: application/json;charset=utf-8
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI.....

我现在收到此错误 - 观众验证失败。观众:“空”。与validationParameters.ValidAudience 不匹配:' http://localhost:54408 ' 或 validationParameters.ValidAudiences: 'null'。

我认为原因是因为我从未在身份验证服务器上设置受众(在 app.UseOpenIdConnectServer(...)),所以我认为它没有将受众信息写入 token 。所以我需要在身份验证服务器上设置一个受众(就像在 IdentityServer3 中所做的那样),但是我在选项对象上找不到可以让我这样做的属性。

AspNet.Security.OpenIdConnect.Server 是否要求身份验证和资源位于同一服务器中?

将 ClaimsPrincipal 放在一起时是否已设置受众,如果是,如何设置?

我是否需要编写自定义受众验证器并将其连接到系统? (我当然希望这个问题的答案是否定的。)

最佳答案

Does AspNet.Security.OpenIdConnect.Server require the auth and resource to be in the same server?



不,您当然可以将这两个角色分开。

正如您已经发现的那样,如果您没有明确指定它,授权服务器将无法确定访问 token 的目标/受众,该 token 在没有 aud 的情况下发出。 OAuth2 不记名中间件默认需要声明。

解决这个问题很简单:只需调用 ticket.SetResources(resources)创建身份验证票证时,授权服务器将确切知道应该在 aud 中添加哪些值(即资源服务器/API)。 claim 。
app.UseOpenIdConnectServer(options =>
{
// Force the OpenID Connect server middleware to use JWT tokens
// instead of the default opaque/encrypted token format used by default.
options.AccessTokenHandler = new JwtSecurityTokenHandler();
});

public override Task HandleTokenRequest(HandleTokenRequestContext context)
{
if (context.Request.IsPasswordGrantType())
{
var identity = new ClaimsIdentity(context.Options.AuthenticationScheme);
identity.AddClaim(OpenIdConnectConstants.Claims.Subject, "unique identifier");

var ticket = new AuthenticationTicket(
new ClaimsPrincipal(identity),
new AuthenticationProperties(),
context.Options.AuthenticationScheme);

// Call SetResources with the list of resource servers
// the access token should be issued for.
ticket.SetResources("resource_server_1");

// Call SetScopes with the list of scopes you want to grant.
ticket.SetScopes("profile", "offline_access");

context.Validate(ticket);
}

return Task.FromResult(0);
}

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
Audience = "resource_server_1",
Authority = "http://localhost:61854"
});

关于asp.net-web-api - 使用 AspNet.Security.OpenIdConnect 分离身份验证和资源服务器 - 受众?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32798244/

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