【问题标题】:jwtBearer bearer token with rc-1 update to ASP.Net 5带有 rc-1 的 jwtBearer 不记名令牌更新到 ASP.Net 5
【发布时间】: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


【解决方案1】:

要使用对称密钥,您需要迁移到 RC2 nightly builds(它不适用于 RC1 本身)。

您可以通过以下方式指定验证 JWT 令牌所需的颁发者密钥(您不需要为此子类化 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);
});

【讨论】:

  • 我要试试这个,你有什么资源可以得到这个信息吗?这听起来绝对完美,我真的希望它有效。我只是好奇你是怎么知道的。这几天我几乎整天都在研究这个
  • 哈哈,没有特别的资源,except this commit(我只是一个长期的 IdentityModel 用户,I use it massively in the projects I work on)。要迁移到 RC2,请在 NuGet 设置中添加 https://www.myget.org/F/aspnetvnext/ 提要,运行 dnvm upgrade -u 以使用最新的 DNX 运行时并在 project.json 中将 rc1-final 替换为 *
  • 我仍然不明白是 rc-1 中忽略了这种支持是一个错误,还是故意的,经过 2 天的挫折,现在一切正常,谢谢
  • 既然你是我的救星,你能帮忙尝试设置 RedirectUrl 吗?检查你stackoverflow.com/questions/34629768/…
  • @ScottSelby 回答 ;)
【解决方案2】:

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;

【讨论】:

  • 你知道 RC1 的任何教程,用于验证 WebAPI 的用户名/密码/令牌。我还没有找到任何最新的东西。我希望我可以为这个新项目构建最新最好的版本,但看起来我越来越有可能不得不回到 ASP.NET 4 w/WebAPI。
  • @pinpoint 确实是专家,如果你能找到他的话
  • @ScottSelby 哈哈,谢谢!我将我的答案作为评论发布在上一篇文章下;)
【解决方案3】:

你可以这样使用它:

 app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                AllowedAudiences = clientIds,
                IssuerSecurityKeyProviders = new IIssuerSecurityKeyProvider[]
                {
                    new SymmetricKeyIssuerSecurityKeyProvider(issuer, key)
                }
            });

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 2018-09-22
    • 2023-03-13
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多