【发布时间】:2016-03-24 18:06:38
【问题描述】:
让我的 asp.net 5 Web 应用程序能够接受 JWT tokens 时遇到了很多麻烦。我的代码已经使用 mvc5 完全正常运行,并且只需要一些帮助将这个代码转换为相同但可以与 mvc6 一起使用。它的设置方式是我的客户端(网站)是一个受信任的应用程序,并使用IssuerSigningToken 来验证受信任的应用程序状态,之后我可以通过 JWT tokens 并从 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
【问题讨论】:
-
我认为您正在寻找 JwtBearerOptions.TokenValidationParameters.IssuerSigningToken。否则,请探索 TokenValidationParameters,因为大部分配置都存在于此。
-
@Tratcher - 谢谢,我有另一个问题试图弄清楚如何设置该参数。看起来接受类型 SecurityKey ,我无法找到使用实际密钥而不是证书的示例
-
在这里提问:github.com/AzureAD/…
标签: c# asp.net-mvc asp.net-core jwt thinktecture-ident-server