Azure AD中基于JWT令牌的身份验证

时间:2017-11-27 13:18:28

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

在验证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的新手吗? 感谢

2 个答案:

答案 0 :(得分:0)

使用nuget包Microsoft.Owin.Security.ActiveDirectory代替:

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

答案 1 :(得分:0)

并且使用以下代码我现在正在使用它...谢谢

 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);
        }