asp.net-mvc - Azure AD 应用程序角色

标签 asp.net-mvc asp.net-web-api asp.net-web-api2 azure-active-directory openid-connect

我正在尝试通过 Azure AD 应用程序角色创建一个 protected Controller 。

这里是 Startup.Auth 的豁免,基本上由 Visual Studio 模板提供:

public void ConfigureAuth(IAppBuilder app)
        {
            ApplicationDbContext db = new ApplicationDbContext();

            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = clientId,
                    Authority = Authority,
                    PostLogoutRedirectUri = postLogoutRedirectUri,

                    Notifications = new OpenIdConnectAuthenticationNotifications()
                    {
                        // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                       AuthorizationCodeReceived = (context) => 
                       {
                           var code = context.Code;
                           ClientCredential credential = new ClientCredential(clientId, appKey);
                           string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                           AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
                           AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                           code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);

                           return Task.FromResult(0);
                       }
                    }
                });
        }

尝试过具有如下属性的 ApiController:

[Authorize(Roles = "Administrators")]
// GET: api/Questions
[ResponseType(typeof(Question))]
public IHttpActionResult GetQuestions()
{
    ....
}

和一个 MVC Controller :

  [Authorize(Roles = "Administrators")]
    public ActionResult Index()
    {
     ....    
    }

在 Azure 应用程序 list 中定义了以下内容:

  "appRoles": [
      { 
        "id": "B4531A9A-0DC8-4015-8CE5-CA1DA1D73754",
        "allowedMemberTypes": ["User"], 
        "description": "Administrators",
        "displayName": "Administrators",
        "value": "Administrators",
        "isEnabled": true,
        "origin": "Application"
      }
  ]

现在执行/api/Questions 的 GET 请求重定向到 https://login.microsoftonline.com并且用户身份验证似乎是成功的,此外本地主机和微软在线之间存在无限循环的请求。见下文:

Request 1

Request 2

Request 3

Request 4

我做错了什么?

使用 [Authorize] 效果很好。

最佳答案

事实证明,应将以下内容添加到 Startup.Auth:

TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
    {
        ValidateIssuer = true,
        // map the claimsPrincipal's roles to the roles claim
        RoleClaimType = "roles",
    }

它实际上是在配置“角色”声明类型,以便将其映射到默认行为。 优秀的解释可在: https://samlman.wordpress.com/2015/03/09/using-roles-in-azure-applications/

关于asp.net-mvc - Azure AD 应用程序角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30084214/

相关文章:

c# - 最大长度不起作用 Web API 2 模型

C# MVC : Trailing equal sign in URL doesn't hit route

c# - ASP.Net MVC 如何生成日期列表

c# - 在 Identity 2.0 中成功实现自定义 UserManager<IUser>

c# - 在运行时禁用 OData V4 元数据和 Controller

c# - 如何在 C# Entity Framework 模型中使用 Json 数据类型?

c# - NopCommerce定制

javascript - ASP.NET MVC 客户端时间从模型转换

c# - 使用 EF 数据库优先应用程序部署 ASP.NET Web API

asp.net-mvc - 使用WEB API时,如何从POST的HttpResponseMessage中提取内容?