gpt4 book ai didi

c# - 带有 rc-1 更新到 ASP.Net 5 的 jwtBearer 承载 token

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:39 24 4
gpt4 key购买 nike

我在让我的 asp.net 5 网络应用能够接受 JWT token 时遇到了很多麻烦。我的代码已经使用 mvc5 完全正常运行,只是需要一些帮助将此代码转换为相同但可与 mvc6 一起使用。它的设置方式是我的客户端(网站)是一个受信任的应用程序并使用 IssuerSigningToken 来验证受信任的应用程序状态,之后我可以传递 JWT token 并从 auth 服务器获取用户和声明详细信息。

旧代码:

public void Configuration(IAppBuilder app)
{
HttpConfiguration httpConfig = new HttpConfiguration();
app.UseJwtBearerAuthentication(new MyJwtOptions());
app.UseWebApi(httpConfig);
ConfigureWebApi(httpConfig);
app.UseWebApi(httpConfig);
}

public class MyJwtOptions : JwtBearerAuthenticationOptions
{
public MyJwtOptions()
{
var issuer = "https://tv.domain.com/trust/domain";
var audience = "https://www.domain.com/";
var key = Convert.FromBase64String("dW8E7DDKW34DDW33jg=");
AllowedAudiences = new[] {audience};
IssuerSecurityTokenProviders = new[] {new SymmetricKeyIssuerSecurityTokenProvider(issuer, key)};
}
}

我能找到的最好的例子是这里 - JwtBearerSample

        app.UseJwtBearerAuthentication(options =>
{
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
// You also need to update /wwwroot/app/scripts/app.js
options.Authority = Configuration["jwt:authority"];
options.Audience = Configuration["jwt:audience"];
});

我不知道我是否接近,我的主要问题是如何添加 IssuerSignerToken ?我正在使用 Thinktecture ,他们似乎还没有任何新的最新示例。有没有人完成我想做的事情?我知道还有其他几个类似的问题,但对那些使用 X.509 Certificates 的回答,如果可能的话,我更愿意为 IssuerSignerToken

更新

我的问题是我过去使用的选项继承自 Microsoft.Owin.Security.JwtBearerAuthenticationOptions 新代码期望 Microsoft.AspNet.Authentication.JwtBearer.JwtBearerOptions

最佳答案

要使用对称 key ,您需要迁移到 RC2 每晚构建版(它不能与 RC1 一起使用)。

以下是您如何指定验证 JWT token 所需的颁发者 key (您不需要为此子类化 JwtBearerOptionsJwtBearerAuthenticationOptions):

var key = Convert.FromBase64String("dW8E7DDKW34DDW33jg=");

app.UseJwtBearerAuthentication(options => {
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;

options.Authority = Configuration["jwt:authority"];
options.Audience = Configuration["jwt:audience"];

options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(key);
});

关于c# - 带有 rc-1 更新到 ASP.Net 5 的 jwtBearer 承载 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34348704/

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