简体   繁体   English

Azure B2C和令牌认证

[英]Azure B2C and authentication with token

I'm creating some webapi in .NET Core with C# . 我正在使用C#.NET Core创建一些webapi。 I'm using Azure B2C as identity manager . 我正在使用Azure B2C作为identity manager When a user does the login, its application receives a token : I think it is a JWT . 当用户进行登录时,其应用程序会收到一个token :我认为它是JWT

How can I validate the token from my webapi against Azure B2C ? 如何针对Azure B2C从我的webapi验证令牌? Can I refresh automatically this token before the expired date? 我可以在过期日期之前自动刷新此令牌吗?

Could this be what you are looking for?: An ASP.NET Core 2.0 web API with Azure AD B2C 这可能是您想要的吗?: 具有Azure AD B2C的ASP.NET Core 2.0 Web API

Basically you handle validation in ASP.NET Core with the JWT Bearer middleware: 基本上,您可以使用JWT Bearer中间件在ASP.NET Core中处理验证:

services.AddAuthentication(options =>
  { 
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; 
  })
  .AddJwtBearer(jwtOptions =>
  {
    jwtOptions.Authority = $"https://login.microsoftonline.com/tfp/{Configuration["AzureAdB2C:Tenant"]}/{Configuration["AzureAdB2C:Policy"]}/v2.0/";
    jwtOptions.Audience = Configuration["AzureAdB2C:ClientId"];
    jwtOptions.Events = new JwtBearerEvents
    {
      OnAuthenticationFailed = AuthenticationFailed
    };
  });

...and validates scopres like this: ...并像这样验证scopres:

var scopes = HttpContext.User.FindFirst("http://schemas.microsoft.com/identity/claims/scope")?.Value;
  if (!string.IsNullOrEmpty(TheScope) && scopes != null && scopes.Split(' ').Any(s => s.Equals(TheScope)))
      // Do stuff
  else 
      // Unauthorized

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM