我有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 - 禁止。
我的门户网站配置如下:
Owner
个角色。Azure active directory
组。我在概述页面上看到,我的用户是该组的成员。我在应用注册清单中添加了角色说明,如下所示。
"appRoles": [
{
"allowedMemberTypes": ["User"],
"displayName": "Admin",
"id": "637b0b37-####-####-####-############",
"isEnabled": true,
"description": "Admin description",
"value": "Admin"
}]
在Azure Active Directory
/ Enterprise applications
/ User and groups
中,我添加了我的用户并为他分配了管理员角色。
Kudu
(App services
/ Advanced tools
)向我显示以下应用设置:
更新:如果我从属性中删除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时 - 类型名称已更改。
有人可以帮忙吗? 感谢。