Beispiel #1
0
    public async Task Seed()
    {
        var user = await _userMgr.FindByNameAsync("ayush");

        if (user == null)
        {
            if (!(await _roleMgr.RoleExistsAsync("Admin")))
            {
                var role = new IdentityRole("Admin");
                role.Claims.Add(new IdentityRoleClaim <string>()
                {
                    ClaimType  = "IsAdmin",
                    ClaimValue = "True"
                });
                await _roleMgr.CreateAsync(role);
            }
            user = new User()
            {
                UserName  = "******",
                FirstName = "Ayush",
                LastName  = "Sinha",
                Email     = "*****@*****.**",
                Password  = "******"
            };

            var userResult = await _userMgr.CreateAsync(user, "ayush@!");

            var roleResult = await _userMgr.AddToRoleAsync(user, "Admin");

            var claimResult = await _userMgr.AddClaimAsync(user, new Claim("SuperUser", "True"));

            if (!userResult.Succeeded || !roleResult.Succeeded || !claimResult.Succeeded)
            {
                throw new InvalidOperationException("Failed to build user or role");
            }
        }

        if (!_ctx.Cars.Any())
        {
            _ctx.AddRange(_sample);
            await _ctx.SaveChangesAsync();
        }
    }
Beispiel #2
0
 public async Task <bool> SaveAllAsync()
 {
     return((await _context.SaveChangesAsync()) > 0);
 }