gpt4 book ai didi

asp.net - 从ASP.NET Core 2.0 API中的JWT token 获取声明

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

我在ASP.NET Core 2.0 Web和API应用程序中使用混合身份验证。既表示Cookie,又表示添加JWT token

该应用程序的Web部分使用Cookie,而在API部分中,我想使用JWT token 。

我的问题是如何从JWT token获得 claim ?在我的Web Controller 中,我可以简单地使用HttpContext.User;来获取存储在cookie中的声明。如何在我想使用JWT token的API方法中处理它?

这是我的AuthenticationBuilder:

public static void MyAuthenticationConfig(IServiceCollection services, IConfiguration configuration)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "myApp_cookie";
options.DefaultChallengeScheme = "myApp_cookie";
})
.AddCookie("myApp_cookie", options =>
{
options.AccessDeniedPath = "/Unauthorized";
options.LoginPath = "/Login";
})
.AddCookie("social_auth_cookie")
.AddOAuth("LinkedIn", options =>
{
options.SignInScheme = "social_auth_cookie";

options.ClientId = "my_client_id";
options.ClientSecret = "my_secret";

options.CallbackPath = "/linkedin-callback";

options.AuthorizationEndpoint = "https://www.linkedin.com/oauth/v2/authorization";
options.TokenEndpoint = "https://www.linkedin.com/oauth/v2/accessToken";
options.UserInformationEndpoint = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,picture-url,picture-urls::(original))";

options.Scope.Add("r_basicprofile");
options.Scope.Add("r_emailaddress");

options.Events = new OAuthEvents
{
OnCreatingTicket = OnCreatingTicketLinkedInCallBack,
OnTicketReceived = OnTicketReceivedCallback
};
})
.AddFacebook(options =>
{
options.SignInScheme = "social_auth_cookie";
options.AppId = "my_app_is";
options.AppSecret = "my_secret";
options.Events = new OAuthEvents
{
OnCreatingTicket = OnCreatingTicketFacebookCallback,
OnTicketReceived = OnTicketReceivedCallback
};
})
.AddGoogle(options =>
{
options.SignInScheme = "social_auth_cookie";
options.ClientId = "my_id.apps.googleusercontent.com";
options.ClientSecret = "my_secret";
options.CallbackPath = "/google-callback";
options.Events = new OAuthEvents
{
OnCreatingTicket = OnCreatingTicketGoogleCallback,
OnTicketReceived = OnTicketReceivedCallback
};
})
.AddJwtBearer("JwtBearer", jwtBearerOptions =>
{
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("my_secret")),

ValidateIssuer = true,
ValidIssuer = "my-api",

ValidateAudience = true,
ValidAudience = "my-client",

ValidateLifetime = true,

ClockSkew = TimeSpan.FromMinutes(5)
};
});
}

最佳答案

通常,JWT的声明会自动添加到ClaimsIdentity中。

来源:
https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/af5e5c2b0100e8348c63e2d2bb45612e2080841e/src/System.IdentityModel.Tokens.Jwt/JwtSecurityTokenHandler.cs#L1110)。

因此,您应该只可以使用基本“Controller”类的“User”属性。

public async Task<IActionResult> Get()
{
// TODO Move 'Claims' extraction code to an extension method
var address = User.Claims.Where('GET THE NEEDED CLAIM');
...
}

从JWT token 获得 claim 我从未遇到任何问题。
但是到目前为止,我只使用了IdentityServer4.AccessTokenValidation。但是在内部,它使用Microsoft JWT Handler afaik。

关于asp.net - 从ASP.NET Core 2.0 API中的JWT token 获取声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134396/

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