【问题标题】:Get Azure Roles in the Web App在 Web 应用程序中获取 Azure 角色
【发布时间】:2018-02-28 10:15:36
【问题描述】:

我在 Azure 中创建了一个 WebApp。我有基于 AzureAD 的身份验证...

实际上所有用户都拥有相同的权限...我需要有一组管理员和世界其他地方。

我看到在我的 Web 应用程序的 Azure 门户中有一个Acces control (IAM),其中列出了一些角色... 我可以在我的应用程序中使用这些角色吗?

实际上我在视图中所做的是:

var isAdmin = User.HasClaim("IsAdmin", true.ToString());

如果我理解正确,名为“基于声明”的身份验证,但我想尝试使用基于角色的身份验证...

我也试过了

var userIdentity = (System.Security.Claims.ClaimsIdentity)User.Identity;
var claims = userIdentity.Claims;
var roleClaimType = userIdentity.RoleClaimType;
var roles = claims.Where(c => c.Type == System.Security.Claims.ClaimTypes.Role).ToList();

但是那个角色列表是空的...

这是public void Configure(IApplicationBuilder app,...中我的Startup.cs验证码

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
    ClientId = Configuration["Authentication:AzureAd:ClientId"],
    Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"],
    CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],
    Events = new OpenIdConnectEvents
    {
        OnTicketReceived = async context =>
        {
            var user = (ClaimsIdentity)context.Ticket.Principal.Identity;
            if (user.IsAuthenticated)
            {
                var firstName = user.FindFirst(ClaimTypes.GivenName).Value;
                var lastName = user.FindFirst(ClaimTypes.Surname).Value;
                var email = user.HasClaim(cl => cl.Type == ClaimTypes.Email) ? user.FindFirst(ClaimTypes.Email).Value : user.Name;
                var connectedOn = DateTime.UtcNow;
                var userId = user.Name;
                var myUser = await repository.GetAsync<Connection>(userId);
                if (myUser == null)
                {
                    myUser = new Connection(userId)
                    {
                        FirstName = firstName,
                        LastName = lastName,
                        Email = email
                    };
                }
                myUser.LastConnectedOn = connectedOn;
                List<Connection> myList = new List<Connection>() { myUser };
                var results = await repository.InsertOrMergeAsync(myList);   
                Claim clm = new Claim("IsAdmin", myUser.IsAdmin.ToString(), ClaimValueTypes.Boolean);
                user.AddClaim(clm);
            }
            return;
        }
      },
    }
});

还有我的 appsettings.json

"Authentication": {
  "AzureAd": {
    "AADInstance": "https://login.microsoftonline.com/",
    "CallbackPath": "/signin-oidc",
    "ClientId": "xxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx",
    "Domain": "mysite.azurewebsites.net",
    "TenantId": "xxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx"
  }
}

【问题讨论】:

    标签: azure authentication asp.net-core-1.1


    【解决方案1】:

    我相信您在门户中观察到的角色与 Web 应用程序的管理有关,而不是对它公开的功能的授权。 要以编程方式使用角色,我建议您查看以下示例,该示例说明如何在 Azure AD 应用程序中设置与您部署为 Web 应用程序的项目相对应的角色。 https://github.com/Azure-Samples/active-directory-dotnet-webapp-roleclaims

    这样您就可以使用属性保护页面(来自控制器中的代码): ``

        [Authorize(Roles = "Admin, Observer, Writer, Approver")]
    

    https://github.com/Azure-Samples/active-directory-dotnet-webapp-roleclaims/blob/master/WebApp-RoleClaims-DotNet/Controllers/TasksController.cs#L17

    您还可以测试具有给定角色的用户: if (User.IsInRole("Admin") || User.IsInRole("Writer")) { ... }

    【讨论】:

    • 对我来说主要问题是如何创建角色...$approverRole = CreateAppRole -Name "Approver" -Description "Approvers have the ability to change the status of tasks." 的用法有点晦涩...
    • 在上面的示例中谈论的是 Manifest 和 web.config 我在 ASP.NET Core 应用程序中没有这样的东西......如果我理解得很好,RoleManager 已经为 .核心框架...另外我使用的是 AzureTable 存储而不是 Sql/CoreFramework...所以我不能使用 IdentityUser...
    • 可以在 Azure 门户中找到清单。找到 Azure Active Directory -> 应用注册 -> 你的应用 -> 清单。
    • @Serge,如果您使用 Visual Studio 创建了一个 ASP.NET Core 应用程序,并单击了 ASP.NET Core 向导中的“更改授权”按钮,则为您创建了一个 Azure AD 应用程序,在您选择的租户中。正如 juunas 所解释的,它在 azure 门户 (portail.azure.com) 中可用。
    • 我不知道微软是怎么想那个向导的,但是一旦项目创建了就不可能访问它......“更改授权”......伤心
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 2021-08-26
    • 1970-01-01
    • 2015-07-17
    • 2016-05-22
    • 2018-10-08
    相关资源
    最近更新 更多