使用 OIDC 向具有 AD 组的控制器授权
Authorize with Controllers with AD Groups using OIDC
我们有一个 Web 应用程序,它使用 OIDC 进行单点登录以进行身份验证
蔚蓝广告。单点登录效果很好,用户可以使用他们的 AD 帐户登录。令牌 it returns 也包含 AD 组。
我想授权我的 MVC 控制器只允许某些组使用某些控制器。
我该如何实施?我可以看到这些组正在以表示为 GUID 的令牌中发回。
我尝试通过 RoleClaimType = "roles",
设置角色声明,但这不起作用。
这是我的代码。
public void ConfigureAuth(IAppBuilder app)
{
FATContext db = new FATContext();
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager(),
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
// Set claimsPrincipal's roles to the roles claim
RoleClaimType = "roles",
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
RedirectToIdentityProvider = (context) =>
{
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);
return Task.FromResult(0);
},
// 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));
return authContext.AcquireTokenByAuthorizationCodeAsync(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
}
}
});
}
例如,我怎样才能让它与我的控制器一起工作,例如
[AuthorizeUser(Roles = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
public ActionResult Index()
{
return View();
}
或检查用户是否处于特定角色,即 User.IsInRole("example ")
在 Azure AD 中,我们可以定义应用程序角色并将角色分配给组。
现在该组的用户将拥有定义角色的声明(示例如下所示)。有关如何在 Azure AD 中为应用程序 create/manage 角色的详细文档,请参阅此 link。
{
"roles": ["admin"]
}
如果您只是想使用基于组的授权,您可以在控制器或操作方法中使用 RoleClaimType = "groups" 和下面的示例代码。有关详细信息,您有类似的信息 here
[Authorize(Roles = "Admin")]
public class TestAdminAccessController : ApiController
{
public ActionResult Index()
{
if (this.User.Identity.IsAuthenticated)
{
if (User.IsInRole("Admin"))
{
// Your logic
}
else
{
// is NOT an admin. No permissions
}
}
else
{
// is NOT Authenticated;
}
}
}
我们有一个 Web 应用程序,它使用 OIDC 进行单点登录以进行身份验证 蔚蓝广告。单点登录效果很好,用户可以使用他们的 AD 帐户登录。令牌 it returns 也包含 AD 组。 我想授权我的 MVC 控制器只允许某些组使用某些控制器。
我该如何实施?我可以看到这些组正在以表示为 GUID 的令牌中发回。
我尝试通过 RoleClaimType = "roles",
设置角色声明,但这不起作用。
这是我的代码。
public void ConfigureAuth(IAppBuilder app)
{
FATContext db = new FATContext();
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager(),
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
// Set claimsPrincipal's roles to the roles claim
RoleClaimType = "roles",
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
RedirectToIdentityProvider = (context) =>
{
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);
return Task.FromResult(0);
},
// 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));
return authContext.AcquireTokenByAuthorizationCodeAsync(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
}
}
});
}
例如,我怎样才能让它与我的控制器一起工作,例如
[AuthorizeUser(Roles = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
public ActionResult Index()
{
return View();
}
或检查用户是否处于特定角色,即 User.IsInRole("example ")
在 Azure AD 中,我们可以定义应用程序角色并将角色分配给组。 现在该组的用户将拥有定义角色的声明(示例如下所示)。有关如何在 Azure AD 中为应用程序 create/manage 角色的详细文档,请参阅此 link。
{
"roles": ["admin"]
}
如果您只是想使用基于组的授权,您可以在控制器或操作方法中使用 RoleClaimType = "groups" 和下面的示例代码。有关详细信息,您有类似的信息 here
[Authorize(Roles = "Admin")]
public class TestAdminAccessController : ApiController
{
public ActionResult Index()
{
if (this.User.Identity.IsAuthenticated)
{
if (User.IsInRole("Admin"))
{
// Your logic
}
else
{
// is NOT an admin. No permissions
}
}
else
{
// is NOT Authenticated;
}
}
}