【发布时间】:2015-06-01 13:26:14
【问题描述】:
我有一个 asp.net mvc 应用程序,我的代码基于这篇文章:http://www.dushyantgill.com/blog/2014/12/10/authorization-cloud-applications-using-ad-groups/
在这个示例代码中: https://github.com/dushyantgill/VipSwapper/tree/master/TrainingPoint
我为全局管理员创建了一个控制器
public class GlobalAdminController : Controller
{
// GET: GlobalAdmin
[AuthorizeUser(Roles = "admin")]
public ActionResult Index()
{
return View();
}
}
这是startup.cs
public void ConfigureAuth(IAppBuilder app)
{
// configure the authentication type & settings
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
// configure the OWIN OpenId Connect options
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = SettingsHelper.ClientId,
Authority = SettingsHelper.AzureADAuthority,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
// we inject our own multitenant validation logic
ValidateIssuer = false,
// map the claimsPrincipal's roles to the roles claim
RoleClaimType = "roles",
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
RedirectToIdentityProvider = (context) =>
{
// This ensures that the address used for sign in and sign out is picked up dynamically from the request
// this allows you to deploy your app (to Azure Web Sites, for example) without having to change settings
// Remember that the base URL of the address used here must be provisioned in Azure AD beforehand.
//string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase;
context.ProtocolMessage.RedirectUri = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path);
context.ProtocolMessage.PostLogoutRedirectUri = new UrlHelper(HttpContext.Current.Request.RequestContext).Action("Index", "Home", null, HttpContext.Current.Request.Url.Scheme);
context.ProtocolMessage.Resource = SettingsHelper.GraphResourceId;
return Task.FromResult(0);
},
// when an auth code is received...
AuthorizationCodeReceived = (context) => {
// get the OpenID Connect code passed from Azure AD on successful auth
string code = context.Code;
// create the app credentials & get reference to the user
ClientCredential creds = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret);
string userObjectId = context.AuthenticationTicket.Identity.FindFirst(System.IdentityModel.Claims.ClaimTypes.NameIdentifier).Value;
// use the ADAL to obtain access token & refresh token...
// save those in a persistent store...
EfAdalTokenCache sampleCache = new EfAdalTokenCache(userObjectId);
AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.AzureADAuthority, sampleCache);
// obtain access token for the AzureAD graph
Uri redirectUri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
AuthenticationResult authResult = authContext.AcquireTokenByAuthorizationCode(code, redirectUri, creds, SettingsHelper.AzureAdGraphResourceId);
if (GraphUtil.IsUserAADAdmin(context.AuthenticationTicket.Identity))
context.AuthenticationTicket.Identity.AddClaim(new Claim("roles", "admin"));
// successful auth
return Task.FromResult(0);
},
AuthenticationFailed = (context) => {
context.HandleResponse();
return Task.FromResult(0);
}
}
});
}
}
如果我使用组织的全局管理员中的用户登录,这将非常有效: http://screencast.com/t/jLVNWGN7MgZR
但是,我创建了另一个组并将用户添加到该组: 该组称为 Company Admin,用户名为 companyadmin@
组 http://screencast.com/t/Y6vueAxjRPo
组成员 http://screencast.com/t/BBRUoOxaD
我创建了另一个控制器:
public class CompanyAdminController : Controller
{
[AuthorizeUser(Roles = "company admin")]
public ActionResult Index()
{
return View();
}
}
我的家庭索引控制器操作中也有这个
public ActionResult Index()
{
if (User.IsInRole("admin"))
{
return RedirectToAction("Index", "GlobalAdmin");
}
if (User.IsInRole("company admin"))
{
return RedirectToAction("Index", "CompanyAdmin");
}
return View();
}
但是,对于公司管理员,User.IsInRole 不会返回 true。 http://screencast.com/t/msVfvUt1g
更新 1
看起来该组确实在索赔中被退回,只是看起来授权没有以正确的方式工作,或者我错过了一些代码。
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-3 adal