将 rc-1 更新为 ASP.Net 5 的 jwtBearer 持有者令牌
jwtBearer bearer token with rc-1 update to ASP.Net 5
我在让我的 asp.net 5 网络应用程序能够接受 JWT tokens
时遇到了很多麻烦。我的代码已经使用 mvc5 完全正常运行,只是需要一些帮助将此代码转换为相同但可与 mvc6 一起使用。它的设置方式是我的客户端(网站)是一个受信任的应用程序并使用 IssuerSigningToken
来验证受信任的应用程序状态,之后我可以通过 JWT tokens
并获取用户和声明从授权服务器返回的详细信息。
旧代码:
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
要使用对称密钥,您需要迁移到 RC2 每晚构建版(它不能与 RC1 一起使用)。
以下是指定验证 JWT 令牌所需的颁发者密钥的方法(您不需要为此子类化 JwtBearerOptions
或 JwtBearerAuthenticationOptions
):
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);
});
Pinpoint 的回答完全正确,但我想我可以补充一点并防止在让它工作时出现数小时令人沮丧的问题。
不要对 属性 Authority
设置任何内容
// even if everything else is properly set you will get 500
// some demos tell you to put CLientId here , that is wrong
options.Authority = "";
你不能直接设置配置,你必须像这样创建一个新的配置对象:
options.Configuration = new Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfiguration()
{
Issuer = "https://tv.domain.com/trust/domain"
};
在 MVC5 中,您可以在控制器中使用 System.Security.Claims
来获取当前用户,如下所示:
var user = ClaimsPrincipal.Current;
这将不再有效,现在您将在控制器中添加:
var user = User.Identity;
你可以这样使用它:
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = clientIds,
IssuerSecurityKeyProviders = new IIssuerSecurityKeyProvider[]
{
new SymmetricKeyIssuerSecurityKeyProvider(issuer, key)
}
});
我在让我的 asp.net 5 网络应用程序能够接受 JWT tokens
时遇到了很多麻烦。我的代码已经使用 mvc5 完全正常运行,只是需要一些帮助将此代码转换为相同但可与 mvc6 一起使用。它的设置方式是我的客户端(网站)是一个受信任的应用程序并使用 IssuerSigningToken
来验证受信任的应用程序状态,之后我可以通过 JWT tokens
并获取用户和声明从授权服务器返回的详细信息。
旧代码:
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
要使用对称密钥,您需要迁移到 RC2 每晚构建版(它不能与 RC1 一起使用)。
以下是指定验证 JWT 令牌所需的颁发者密钥的方法(您不需要为此子类化 JwtBearerOptions
或 JwtBearerAuthenticationOptions
):
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);
});
Pinpoint 的回答完全正确,但我想我可以补充一点并防止在让它工作时出现数小时令人沮丧的问题。
不要对 属性 Authority
// even if everything else is properly set you will get 500
// some demos tell you to put CLientId here , that is wrong
options.Authority = "";
你不能直接设置配置,你必须像这样创建一个新的配置对象:
options.Configuration = new Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfiguration()
{
Issuer = "https://tv.domain.com/trust/domain"
};
在 MVC5 中,您可以在控制器中使用 System.Security.Claims
来获取当前用户,如下所示:
var user = ClaimsPrincipal.Current;
这将不再有效,现在您将在控制器中添加:
var user = User.Identity;
你可以这样使用它:
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = clientIds,
IssuerSecurityKeyProviders = new IIssuerSecurityKeyProvider[]
{
new SymmetricKeyIssuerSecurityKeyProvider(issuer, key)
}
});