具有角色的 Azure mvc owin 授权在 azure 门户中不起作用

问题描述 投票:0回答:0

我有 sample 如何配置简单的

authorization
以从我自己的
Azure
active directory
租户登录用户。 我只在这段代码中做了一个更改,我通过我的 AuthorizeAttribute 实现标记了 HomeController,因为用户不能在没有身份验证/授权的情况下进入我的页面。

[Authorize(Roles = "Admin")]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

authorization
配置如下所示。

private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];

string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions());

    app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                RedirectUri = postLogoutRedirectUri,
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = context =>
                    { 
                        context.HandleResponse();
                        context.Response.Redirect("/Error?message=" + context.Exception.Message);
                        return Task.FromResult(0);
                    }
                }
            });
}

此代码在我的本地计算机上运行良好(我更改了 https://localhost:port 上的 PostLogoutRedirectUri 配置参数以在本地运行)。我能够获取

Azure Active Directory
用户数据并由管理员
role
进行属性过滤。此外,像 IsInRole 这样的方法也很有效。 但是当我将此代码部署到 Azure 门户时,我在浏览器中看到以下错误:您无权查看此目录或页面。.

当我打开门户日志流时,我看到带有以下文本的 403 错误页面标记:HTTP 错误 403.60 - 禁止.

我的门户配置如下:

  1. 在应用程序服务列表中,我的应用程序名称为:WebApp-OpenIDConnect。
  2. 在访问控制(IAM)中-我看到我的用户具有
    Owner
    角色。
  3. 我已经配置了身份验证/授权部分并启用了应用服务身份验证。此外,我在 Action to take when request is not authenticated 设置中设置了 Log in with Azure Active Directory 值,并且我已经通过创建新的 Azure Add App 配置了 Azure Active Directory。
  4. 我已经有一个名为“
    Admin
    ”的Azure active directory组。我在概览页面上看到,我的用户是该组的成员。
  5. 我在应用程序注册清单中添加了角色描述,如下所示。

    "appRoles": [
    {
    "allowedMemberTypes": ["User"],
    "displayName": "Admin",
    "id": "637b0b37-####-####-####-############",
    "isEnabled": true,
    "description": "Admin description",
    "value": "Admin"      
    }]
    
  6. Azure Active Directory
    /
    Enterprise applications
    /
    User and groups
    中,我添加了我的用户并为他分配了管理员角色。

  7. 我已经检查过我的 app.config 中的 ClientId 与应用程序注册页面中的应用程序 ID 具有相同的 guid。
  8. Kudu
    (
    App services
    /
    Advanced tools
    ) 向我显示以下应用程序设置:
    • WEBSITE_AUTH_DEFAULT_PROVIDER“AzureActiveDirectory”

更新: 如果我从属性中删除 Roles="Admin" 参数(仅保留 Authorize()),授权工作正常,甚至声明集合在 roles 声明中包含

Admin
角色值。但是当我返回 Roles 参数时,403 错误再次出现。

更新 2: 我认为问题与声明角色类型的差异有关。当我在本地运行我的应用程序时。我有带有命名空间的声明类型名称和单一形式:http://schemas.microsoft.com/ws/2008/06/identity/claims/role(值为Admin),但是当我在 Azure 门户中运行我的应用程序,我看到声明类型名称为复数形式且没有名称空间:roles(值为 Admin 以及之前的值)。

UPDATE3我尝试在我的配置部分中更新RoleClaimType名称,如下所示,试图更改声明集合中的角色声明类型与

ClaimsIdentity
对象中的预期RoleClaimType之间的不匹配:

app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                RedirectUri = postLogoutRedirectUri,
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = context =>
                    { 
                        context.HandleResponse();
                        context.Response.Redirect("/Error?message=" + context.Exception.Message);
                        return Task.FromResult(0);
                    }
                },
                TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                {
                    ValidateIssuer = false,
                    RoleClaimType = "roles",
                }
            });

但是在将我的应用程序部署到 Azure 之后,声明类型仍然具有旧值:RoleClaimType = http://schemas.microsoft.com/ws/2008/06/identity/claims/role 并且没有改变通过我的配置更改。当我在本地运行时 - 类型名称已更改。

有人可以帮忙吗? 谢谢。

c# asp.net-mvc azure-active-directory owin azureportal
© www.soinside.com 2019 - 2024. All rights reserved.