Azure B2C:我如何获得;“集团”;JWT令牌中的索赔
在Azure B2C中,我曾经能够通过以下方式在我的JWT代币中获得“组”声明:Azure B2C:我如何获得;“集团”;JWT令牌中的索赔,azure,jwt,azure-ad-b2c,Azure,Jwt,Azure Ad B2c,在Azure B2C中,我曾经能够通过以下方式在我的JWT代币中获得“组”声明: 打开旧式Azure管理器() 向B2C注册我的申请 下载应用程序的B2C清单 在清单中,将“groupMembershipClaims”条目更改为 “groupMembershipClaims”:“SecurityGroup” 再次上载更改的B2C清单 问题 这在过去(我相信大约一个月前…)很有效,但现在不行了。详情见下文 我已经试过了 计划A:使用Azure Manager 遵循上面已知的好食谱 不幸的是,
- 打开旧式Azure管理器()
- 向B2C注册我的申请
- 下载应用程序的B2C清单
- 在清单中,将“groupMembershipClaims”条目更改为 “groupMembershipClaims”:“SecurityGroup”
- 再次上载更改的B2C清单
这些天我如何在我的JWT代币中获得“团体”索赔?恐怕是Z计划。我不知道他们为什么不退回,但它现在已经过期了 我就是这样做的。当用户通过身份验证时查询组,您也可以按自己的方式进行-只需在需要时进行查询。这取决于您的用例
公共部分类启动
{
public void ConfigureAuth(IAppBuilder应用程序)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.usekentorovicookiesaver();
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
LoginPath=新路径字符串(“/account/unauthorized”),
CookieSecure=CookieSecureOption。始终,
ExpireTimeSpan=从分钟(20)开始的时间跨度,
slidengexpiration=true,
CookieHttpOnly=true
});
//为每个策略配置OpenID连接中间件
app.UseOpenIdConnectAuthentication(CreateOptionsFromPolicy(Globals.SignInPolicyId));
}
私有OpenIdConnectAuthenticationOptions CreateOptionsFromPolicy(字符串策略)
{
返回新的OpenIdConnectAuthenticationOptions
{
//对于每个策略,为OWIN提供特定于策略的元数据地址,以及
//将身份验证类型设置为策略的id
MetadataAddress=string.Format(Globals.AadInstance、Globals.TenantName、policy),
AuthenticationType=策略,
AuthenticationMode=AuthenticationMode.Active,
//这些是标准的OpenID连接参数,其值来自web.config
ClientId=Globals.clientdforlogin,
RedirectUri=Globals.RedirectUri,
PostLogoutRedirectUri=Globals.RedirectUri,
通知=新的OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed=AuthenticationFailed,
SecurityTokenValidated=SecurityTokenValidated
},
Scope=“openid”,
ResponseType=“id\u令牌”,
//此项是可选的-用于在导航栏中显示用户名。
TokenValidationParameters=新的TokenValidationParameters
{
NameClaimType=“name”,
}
};
}
专用异步任务SecurityTokenValidated(SecurityTokenValidatedNotification令牌)
{
var groups=await\u metaDataService.GetGroups(token.AuthenticationTicket.Identity.FindFirst(“http://schemas.microsoft.com/identity/claims/objectidentifier(二)价值;;
if(groups?.Value!=null&&groups.Value.Any())
{
foreach(groups.Value.ToList()中的IGroup组)
{
token.AuthenticationTicket.Identity.AddClaim(
新声明(ClaimTypes.Role、group.DisplayName、ClaimValueTypes.String、“图形”);
}
}
}
//用于避免黄屏死亡
私有任务身份验证失败(身份验证失败通知)
{
notification.HandleResponse();
if(notification.Exception.Message==“访问被拒绝”)
{
通知.响应.重定向(“/”);
}
其他的
{
notification.Response.Redirect(“/error?message=“+notification.Exception.message”);
}
返回Task.FromResult(0);
}
}
我的GetGroups
方法只是查询
然后,我有一个简单的助手方法来确定用户是否处于角色中:
publicstaticbooluserisinrole(IPrincipal用户,字符串roleName)
{
风险值索赔