【问题标题】:JWT Token based Authentication in Azure ADAzure AD 中基于 JWT 令牌的身份验证
【发布时间】:2018-05-10 18:00:10
【问题描述】:

在验证 Azure AD 颁发的令牌时,我在 startup.css 中出现错误

JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IA‌​pplicationBuilder, JwtBearerOptions)' 已过时:'参见 go.microsoft.com/fwlink/?linkid=845470';

我的代码是

 app.UseJwtBearerAuthentication(new JwtBearerOptions
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                Authority = String.Format(Configuration["AzureAd:AadInstance"], Configuration["AzureAD:Tenant"]),
                Audience = Configuration["AzureAd:Audience"],
            });

我是 Azure 和 Web API 的新手,有什么建议吗? 谢谢

【问题讨论】:

  • 您是否阅读了链接:github.com/aspnet/Security/issues/1310?身份验证在 2.0 中发生了一些变化,这种定义身份验证中间件的方式现在已经过时了。
  • 嗨@juunas 感谢您的回复,是的,我已经检查了此链接,但无法弄清楚如何处理我的问题。如果您有一些代码或更多信息,那将很有帮助。

标签: asp.net-mvc asp.net-web-api jwt azure-active-directory asp.net-core-2.0


【解决方案1】:

改用 nuget 包 Microsoft.Owin.Security.ActiveDirectory

app.UseWindowsAzureActiveDirectoryBearerAuthentication(new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
    TokenValidationParameters = new TokenValidationParameters
    {
        ValidAudience = ConfigurationManager.AppSettings["AzureAd:Audience"]
    },
    Tenant = ConfigurationManager.AppSettings["AzureAd:AADInstance"]
});

【讨论】:

  • 感谢您的回复。我应该使用 Wot 命名空间来使用 UseWindowsAzureActiveDirectoryBearerAuthentication。我收到错误 iapplicationbuilder 不包含 UseWindowsAzureActiveDirectoryBearerAuthentication 的定义
  • 您需要安装Microsoft.Owin.Security.ActiveDirectory nuget 包。 IAppBuilder 上的扩展方法在 WindowsAzureActiveDirectoryBearerAuthenticationExtensions 类中。
  • 怎么样@ashishjayara ?
  • 嗨 ..Apologies for late reply我做了同样的事情并且它工作但现在它在 TokenValidationParameters 上给出另一个错误说它声称在 System.Identitymodel.Tokens.jwt 但找不到@peco
  • 我不确定我是否理解,但您可以打开 ADAL 的日志记录。这个答案帮助了我一次stackoverflow.com/a/33272736/1157185
【解决方案2】:

通过下面的代码,我现在可以使用它了..谢谢

 public void ConfigureServices(IServiceCollection services)
        {

            services.AddAuthentication(options =>
            {
                options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            })
          .AddJwtBearer(jwtOptions =>
          {
              jwtOptions.Authority = String.Format(Configuration["Logging:AzureAd:AadInstance"], Configuration["Logging:AzureAD:Tenant"]);
              jwtOptions.Audience = Configuration["Logging:AzureAd:Audience"];
              jwtOptions.Events = new JwtBearerEvents
              {
                  OnAuthenticationFailed = AuthenticationFailed
              };
          });

            services.AddMvc();
        }

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseAuthentication();
            app.UseMvc();
        }

        private Task AuthenticationFailed(AuthenticationFailedContext arg)
        {
            // For debugging purposes only!
            var s = $"AuthenticationFailed: {arg.Exception.Message}";
            arg.Response.ContentLength = s.Length;
            arg.Response.Body.Write(Encoding.UTF8.GetBytes(s), 0, s.Length);
            return Task.FromResult(0);
        }

【讨论】:

    猜你喜欢
    • 2020-04-23
    • 1970-01-01
    • 2020-01-12
    • 2016-09-21
    • 2016-06-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2019-03-13
    相关资源
    最近更新 更多