asp.net-mvc - 登录后在哪里向当前委托人添加额外的声明?
问题描述
我有一个 asp.net mvc 应用程序,并且正在使用以下示例中的代码:https ://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi
用户登录后,我想在主体声明中添加系统管理员角色声明,我能想到的最佳位置是Startup.Auth.cs:
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification notification)
{
try
{
var userEmail = notification.AuthenticationTicket.Identity.
Claims.SingleOrDefault(x => x.Type == ClaimTypes.Email)?.Value;
if (userEmail != null)
{
using (var ctx = new DbContext())
{
var currentUser = ctx.Users.SingleOrDefault(u => u.Email == userEmail);
if (currentUser != null && currentUser.IsAdmin)
{
notification.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimTypes.Role, Common.Configuration.RoleAdministratorName));
}
}
}
这似乎可行,但是在startup.auth中为此实例化一个新的数据库上下文是不合适的。这是正常的做法吗?
解决方案
推荐阅读
- prolog - 根据一些规范在 Prolog 中创建一个列表
- c - 在 xv6 中实现自旋锁功能,以便能够从用户级别使用 API
- azure - QnA Maker 在每次培训中给出不同的分数
- python - 如何在我的代码中修复“UnboundLocalError:赋值前引用的局部变量 'dc'”?
- android - 无需用户操作即可取消订阅
- c++ - 带有许多点运算符的 C++ 函数调用
- java - 打开 jar 文件时,在 JAVA 中更改外观和感觉不起作用
- swift - 在“SIMD”上引用运算符函数“*”要求“_.Scalar”符合“FloatingPoint”
- python - 范数等于一的随机启动向量?
- python - 在预先存在的列表上使用 np.r_ 函数