【问题标题】:Token Based Authentication in Asp.Net Core API with token from Azure AD gotten through ADALAsp.Net Core API 中基于令牌的身份验证,通过 ADAL 获得来自 Azure AD 的令牌
【发布时间】:2017-08-18 14:08:28
【问题描述】:

我正在使用 Asp.net core 1.1Angular 4 开发应用程序。

在一个解决方案中,我有我的 API 和 Angular 应用程序。所以我不需要单独托管它们或启用 cors。要从客户端进行调用,我只需在 angular 的 http 方法中传递“/mycontroler”。

此应用的要求之一是使用使用 Azure AD 登录,但我找不到任何可以帮助我实现此流程的清晰且最新的内容。

到目前为止,我使用 Adal 从前端登录,我能够接收用户信息和令牌。那么,我现在如何在我的 api 中使用这个令牌来限制对资源的访问呢?

我的 API 启动代码是这样的:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
  app.Use(async (context, next) =>
  {
    await next();
    if (context.Response.StatusCode == 404 &&
       !Path.HasExtension(context.Request.Path.Value) &&
       !context.Request.Path.Value.StartsWith("/api/"))
    {
      context.Request.Path = "/index.html";
      await next();
    }
  });
  app.UseExceptionHandler(errorApp =>
  {
    errorApp.Run(async context =>
    {
      context.Response.StatusCode = 500; // or another Status accordingly to Exception Type
      context.Response.ContentType = "application/json";

      var error = context.Features.Get<IExceptionHandlerFeature>();
      if (error != null)
      {
        var ex = error.Error;

        await context.Response.WriteAsync(ex.Message, Encoding.UTF8);
      }
    });
  });

  app.UseJwtBearerAuthentication(new JwtBearerOptions
  {
    Authority = $"{Configuration["Authentication:AzureAd:AADInstance"]}{Configuration["Authentication:AzureAd:TenantId"]}",
    Audience = Configuration["Authentication:AzureAd:ClientId"],
    TokenValidationParameters =
            new Microsoft.IdentityModel.Tokens.TokenValidationParameters
            {
              ValidIssuer =
                $"{Configuration["Authentication:AzureAd:AADInstance"]}{Configuration["Authentication:AzureAd:TenantId"]}/v2.0"
            }
  });

  app.UseMvcWithDefaultRoute();
  app.UseDefaultFiles();
  app.UseStaticFiles();

}

} }

但我不知道在我的控制器中具体做什么来检查用户权限和其他信息,例如他们所在的组。

在大多数情况下,我发现令牌是使用身份在 API 本身中发出的,但我的情况不同,因为令牌是由 AAD 发出

我在这里阅读了很多文章和帖子,但找不到任何可以帮助我的东西。

【问题讨论】:

    标签: angular asp.net-core azure-active-directory adal


    【解决方案1】:

    您可以使用 Azure AD 在云应用程序中使用基于角色的访问控制。在 RBAC 中,角色是权限的集合。角色可以授予用户或用户(组)的集合。请阅读this article 获取教程和代码示例。

    要获取用户的组,您可以使用 Azure AD 组声明。配置您的应用程序以接收组声明:

    1. 在您的应用程序页面中,单击“清单”以打开内联清单编辑器。
    2. 通过找到“groupMembershipClaims”设置来编辑清单,并将其值设置为“All”(如果您对分发列表不感兴趣,则设置为“SecurityGroup”)。

    3. 保存清单。

    在应用中授权后,您可以找到当前登录用户的群组信息。请点击herehere了解有关团体索赔的更多详细信息。

    当然,您也可以随时调用 microsft graph api 来获取用户的组信息或应用角色信息。请参阅使用 microsoft graph api 的List memberOf 操作。

    【讨论】:

    • 非常感谢南宇的回答!我实际上已经想通了,并来这里回答我自己的问题,但是,您提供的文章肯定会有用!还有另一个问题,虽然..我仍在尝试弄清楚如何在我的应用程序中使用自定义角色。我在清单文件中添加了两个角色,但我不知道如何使用新的 Azure 门户将这些角色分配给我的组。当我转到我的应用程序的 IAM 部分时,我在角色列表中看不到我的自定义角色。
    • 在 azure 门户中,通过单击左侧导航中的 Active Directory 导航到您的租户,单击 Enterprise applications-->选择您的应用程序-->单击 Users and groups --> 单击 @987654327 @按钮添加您要分配角色的用户 --> 选择一个用户并选择您在清单中定义的角色。
    猜你喜欢
    • 2020-04-23
    • 2018-05-10
    • 2015-05-16
    • 1970-01-01
    • 2021-10-28
    • 2017-05-19
    • 2019-10-14
    • 2015-08-13
    • 2016-06-09
    相关资源
    最近更新 更多