首页 > 解决方案 > 登录后在哪里向当前委托人添加额外的声明?

问题描述

我有一个 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中为此实例化一个新的数据库上下文是不合适的。这是正常的做法吗?

标签: asp.net-mvcazure-ad-b2cazure-ad-graph-api

解决方案


推荐阅读