简体   繁体   English

MVC6 / EF7:播种主用户和声明

[英]MVC6/EF7: Seeding Master User and Claims

I'm writing a simple MVC6 app using EF7 for the data side, and am running into some problems with setting user and claims. 我正在使用EF7编写一个简单的MVC6应用程序,用于数据端,并且在设置用户和声明时遇到了一些问题。 Specifically, I have a database seeding routine to populate the identity tables in the database with a particular user (me) who has specific claims. 具体来说,我有一个数据库种子例程,用于使用具有特定声明的特定用户(我)来填充数据库中的身份表。 This seeding method gets called from within startup.cs. 此种子方法从startup.cs中调用。

It's pretty simple: 很简单:

    public static async Task CreateMasterUser( UserManager<ApplicationUser> userMgr, ILogger logger )
    {
        var user = await userMgr.FindByEmailAsync("my email");

        if( user == null )
        {
            user = new ApplicationUser()
            {
                Email = "my email",
                FirstName = "blah",
                LastName = "blah",
                UserName = "Blah",
            };

            IdentityResult result = await userMgr.CreateAsync(user, "mypw");

            if( !result.Succeeded )
            {
                logger.LogCritical("Failed to create master user. Messages were: " + String.Join("; ", result.Errors.Select(x => x.Description)));
                return;
            }
        }

        await AddClaim(user, userMgr, "user.add", "allowed");
        await AddClaim(user, userMgr, "user.delete", "allowed");
        await AddClaim(user, userMgr, "user.edit.self", "allowed");
        await AddClaim(user, userMgr, "user.edit.all", "allowed");
        await AddClaim(user, userMgr, "user.confirm", "allowed");
        await AddClaim(user, userMgr, "content.add", "allowed");
        await AddClaim(user, userMgr, "content.view", "allowed");
        await AddClaim(user, userMgr, "content.edit", "allowed");
        await AddClaim(user, userMgr, "content.delete", "allowed");
    }

    private static async Task AddClaim( ApplicationUser user, UserManager<ApplicationUser> userMgr, string claim, string value )
    {
        if( user.Claims.SingleOrDefault(x => x.ClaimType == claim) == null )
            await userMgr.AddClaimAsync(user, new Claim(claim, value));
    }

But there's a problem with claims section: every time the site starts up, a new set of claims gets added for me (ie, duplicate claims are being created for the same user). 但是索赔部分存在一个问题:每次站点启动时,都会为我添加一组新的索赔(即,为同一用户创建重复的索赔)。

When I step through the code, user.Claims never has any entries, even though the database (sql server) shows entries in AspNetUserClaims for the relevant user. 当我单步执行代码时,即使数据库(sql服务器)在AspNetUserClaims中为相关用户显示条目,user.Claims也永远不会包含任何条目。

It appears like the Claims collection is not being populated from the database by FindEmailAsync. 好像FindEmailAsync没有从数据库中填充Claims集合。

How do I make that happen? 我如何做到这一点?

Take a look at GetClaimsAsync method of the UserManager. 看一下UserManager的GetClaimsAsync方法。 Explicitly populate your Claims collection. 明确填充您的Claims集合。

user.Claims = await userMgr.GetClaimsAsync(user);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM