针对ProtectedWebWebi的Azure AD身份验证

时间:2020-06-24 12:28:34

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

我下载了守护程序应用程序的Azure示例,并且在调试代码的末尾有一个承载令牌。我坚持的事情是使用带有此令牌的Web API进行身份验证。这是.NET Core 2.2中的

    public void ConfigureServices(IServiceCollection services)
    {
        // This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
        // By default, the claims mapping will map claim names in the old format to accommodate older SAML applications.
        // 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' instead of 'roles'
        // This flag ensures that the ClaimsIdentity claims collection will be built from the claims in the token
        JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

        services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration);

        services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
        {
            //options.TokenValidationParameters.RoleClaimType = "roles";
        });

        //// Creating policies that wraps the authorization requirements
        services.AddAuthorization(options =>
        {
            options.AddPolicy("AdminUsers", policy => policy.RequireRole("AdminUsers"));
        });

        services.AddControllers();
    }

    [HttpGet]
    [Authorize]
    public IActionResult Get()
    {
        return Ok(TodoStore.Values);
    }

我取消了角色,只是将其提供给所有人,不确定是否需要在此处添加组授权才能使其正常工作。我无法立即使用它。我正在考虑修改其中的一些使其起作用。我们只需要进行身份验证,然后使用他们的电子邮件来根据他们所在的应用程序来获取角色。

谢谢。

1 个答案:

答案 0 :(得分:0)

请参阅此Microsoft documentation

对于守护程序应用,您调用的Web API需要预先批准。 守护程序应用程序无增量许可。 (没有用户 租户管理员需要事先同意 应用程序和所有API权限。

并且有一个GitHub sample用于使用Web Api的守护程序应用程序,如果您要验证由守护程序调用的API中的应用程序角色,请仔细阅读此documentation