gpt4 book ai didi

angularjs - 使用 OpenIdConnect 和 angularjs : Bearer was forbidden 基于 Asp.net 核心 token 的声明身份验证

转载 作者:行者123 更新时间:2023-12-01 00:41:09 26 4
gpt4 key购买 nike

我正在使用带有 OpenIdConnectServer 的 Asp.net 核心 rc2。我正在使用带有augular-oauth2的angular 1.x。几天后,我的错误已经偏离了

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:54275/api/Account/Username  
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware:Information: Successfully validated the token.
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware:Information: HttpContext.User merged via AutomaticAuthentication from authenticationScheme: Bearer.
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware:Information: AuthenticationScheme: Bearer was successfully authenticated.
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed for user: .
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Warning: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes (Bearer).
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware:Information: AuthenticationScheme: Bearer was forbidden.

我的 ConfigureServices 包括
services.AddAuthorization(options =>
{
options.AddPolicy("UsersOnly", policy =>
{
policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);
policy.RequireClaim("role");
});
});

我的配置有
app.UseWhen(context => context.Request.Path.StartsWithSegments(new PathString("/api")), branch =>
{
branch.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = false,

Audience = "http://localhost:54275/",
Authority = "http://localhost:54275/",
TokenValidationParameters = new TokenValidationParameters
{
ValidAudience = "client1",
//ValidAudiences = new List<string> { "", "empty", "null"}
}
});
});

app.UseOpenIdConnectServer(options =>
{
options.AuthenticationScheme = OpenIdConnectServerDefaults.AuthenticationScheme;
options.Provider = new SimpleAuthorizationServerProvider();
options.AccessTokenHandler = new JwtSecurityTokenHandler();
options.ApplicationCanDisplayErrors = true;
options.AllowInsecureHttp = true;
options.TokenEndpointPath = new PathString("/oauth2/token");
options.LogoutEndpointPath = new PathString("/oauth2/logout");
options.RevocationEndpointPath = new PathString("/oauth2/revoke");
options.UseJwtTokens();
//options.AccessTokenLifetime = TimeSpan.FromHours(1);
});

我的授权属性在 Controller 上定义为
[Authorize(Policy = "UsersOnly", ActiveAuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme), Route("api/Account")]

我将 token 存储为 cookie,并使用 angular 中的 http 拦截器将其附加到请求。

我生成 token
public override async Task GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsContext context)
{
// validate user credentials (demo mode)
// should be stored securely (salted, hashed, iterated)
using (var con = new SqlConnection(ConnectionManager.GetDefaultConnectionString()))
{
if (!Hashing.ValidatePassword(context.Password, await con.ExecuteScalarAsync<string>("SELECT PassHash FROM dbo.Users WHERE Username = @UserName", new { context.UserName })))
{
context.Reject(
error: "bad_userpass",
description: "UserName/Password combination was invalid."
);
return;
}

// create identity
var id = new ClaimsIdentity(context.Options.AuthenticationScheme);
id.AddClaim(new Claim("sub", context.UserName));
id.AddClaim(new Claim("role", "user"));

// create metadata to pass on to refresh token provider
var props = new AuthenticationProperties(new Dictionary<string, string>
{
{"as:client_id", context.ClientId}
});
var ticket = new AuthenticationTicket(new ClaimsPrincipal(id), props,
context.Options.AuthenticationScheme);
ticket.SetAudiences("client1");
//ticket.SetScopes(OpenIdConnectConstants.Scopes.OpenId, OpenIdConnectConstants.Scopes.Email, OpenIdConnectConstants.Scopes.Profile, "api-resource-controller");
context.Validate(ticket);
}
}

过去三天我一直在解决这个问题,我意识到此时我可能由于 sleep 不足而遗漏了一些明显的东西。任何帮助,将不胜感激。

最佳答案

您看到的错误可能是由两个因素引起的:

  • 您没有将明确的目的地附加到您的自定义 role声明,因此它永远不会在访问 token 中序列化。您可以找到有关此安全功能的更多信息 on this other SO post .
  • policy.RequireClaim("role"); OTB 可能不起作用,因为 IdentityModel 使用内部映射将众所周知的 JWT 声明转换为其 ClaimTypes等效:这里,role很可能会被 http://schemas.microsoft.com/ws/2008/06/identity/claims/role 取代( ClaimTypes.Role )。我建议使用 policy.RequireRole("user")反而。

  • 还值得注意的是手动存储 client_id没有必要,因为 OpenID Connect 服务器中间件已经为您完成了。

    您可以使用 ticket.GetPresenters() 检索它,返回授权演示者列表(这里是客户端标识符)。请注意,它还会自动确保客户端 B 无法使用颁发给客户端 A 的刷新 token ,因此您不必在自己的代码中进行此检查。

    关于angularjs - 使用 OpenIdConnect 和 angularjs : Bearer was forbidden 基于 Asp.net 核心 token 的声明身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37771125/

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