Exemplo n.º 1
0
 protected override IdentityRoleClaim <string> CreateRoleClaim(PandaUserRole role, Claim claim) =>
 new IdentityRoleClaim <string>
 {
     RoleId     = role.Id,
     ClaimType  = claim.Type,
     ClaimValue = claim.Value,
 };
Exemplo n.º 2
0
        public static void Seed(Panda2DbContext context)
        {
            var roleNames = new List <string>()
            {
                "Administrator",
                "User",
                "Guest"
            };

            var contextRoles = context.Roles.Select(r => r.Name).ToList();

            if (roleNames.Count > contextRoles.Count)
            {
                var roles = new List <PandaUserRole>();

                foreach (var roleName in roleNames)
                {
                    if (!contextRoles.Contains(roleName))
                    {
                        var role = new PandaUserRole(roleName);
                        roles.Add(role);
                    }
                }

                context.Roles.AddRange(roles);
                context.SaveChanges();
            }


            var statusNames = new List <string>()
            {
                "Pending",
                "Shipped",
                "Delivered",
                "Acquired",
                "Test"
            };

            var contextStatuses = context.Statuses.Select(s => s.Name).ToList();

            if (statusNames.Count > contextStatuses.Count)
            {
                var statuses = new List <Status>();

                foreach (var statusName in statusNames)
                {
                    if (!contextStatuses.Contains(statusName))
                    {
                        var status = new Status(statusName);
                        statuses.Add(status);
                    }
                }

                context.Statuses.AddRange(statuses);
                context.SaveChanges();
            }
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            IdentityResult result;

            returnUrl = returnUrl ?? this.Url.Content("~/");
            if (this.ModelState.IsValid)
            {
                var user = new PandaUser {
                    UserName = this.Input.Email, Email = this.Input.Email
                };
                if (!this.userManager.Users.Any())
                {
                    result = await this.userManager.CreateAsync(user, this.Input.Password);

                    var role = new PandaUserRole("Admin");
                    await this.userManager.AddToRoleAsync(user, "Admin");
                }
                else
                {
                    result = await this.userManager.CreateAsync(user, this.Input.Password);

                    await this.userManager.AddToRoleAsync(user, "User");
                }


                if (result.Succeeded)
                {
                    this.logger.LogInformation("User created a new account with password.");

                    var code = await this.userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = this.Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: this.Request.Scheme);

                    //   await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                    //      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await this.signInManager.SignInAsync(user, isPersistent : false);

                    return(this.LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    this.ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(this.Page());
        }
Exemplo n.º 4
0
 public static void SeedRoles(RoleManager <PandaUserRole> roleManager)
 {
     if (!roleManager.RoleExistsAsync("User").Result)
     {
         PandaUserRole role = new PandaUserRole();
         role.Name = "User";
         IdentityResult roleResult = roleManager.CreateAsync(role).Result;
     }
     if (!roleManager.RoleExistsAsync("Admin").Result)
     {
         PandaUserRole role = new PandaUserRole();
         role.Name = "Admin";
         IdentityResult roleResult = roleManager.CreateAsync(role).Result;
     }
 }
Exemplo n.º 5
0
 protected override IdentityUserRole <string> CreateUserRole(PandaUser user, PandaUserRole role)
 {
     return(new IdentityUserRole <string> {
         RoleId = role.Id, UserId = user.Id
     });
 }