具有角色的Azure mvc owin授权在azure portal

时间:2018-03-27 18:31:16

标签: asp.net-mvc azure owin azure-active-directory azureportal

我有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. 在App Services列表中,我的应用程序名称为:WebApp-OpenIDConnect。
  2. 在访问控制(IAM)中 - 我看到我的用户有Owner个角色。
  3. 我已配置身份验证/授权部分并启用了应用服务身份验证。此外,我在操作中设置使用Azure Active Directory登录值,以便在请求未经过身份验证时设置以及我已配置Azure Active Directory并创建新Azure添加App。
  4. 我已经拥有名为“管理员”的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与应用程序注册页面中的Application ID具有相同的guid。
  8. KuduApp services / Advanced tools)向我显示以下应用设置:
    • WEBSITE_AUTH_DEFAULT_PROVIDER“AzureActiveDirectory”
  9. 更新:如果我从属性中删除Roles =“Admin”参数(仅保留Authorize()),则授权可正常工作,甚至声明集合也包含 Admin 角色值roles声称。但是当我返回Roles参数时,再次出现403错误。

    UPDATE2:我认为问题与声明角色类型的差异有关。当我在本地运行我的应用程序。我的声明类型名称带有命名空间并采用单一形式: http://schemas.microsoft.com/ws/2008/06/identity/claims/role (值为 Admin ),但是当我在azure portal中运行我的应用程序时,我请参阅复数形式且没有命名空间的声明类型名称:角色(值为 Admin 以及之前的版本)。

    UPDATE3 我尝试在配置部分中更新 RoleClaimType 名称,如下所示,尝试更改声明集合中的角色声明类型与{C中的预期RoleClaimType之间的不匹配{1}}对象:

    ClaimsIdentity

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

    有人可以帮忙吗? 感谢。

0 个答案:

没有答案