public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new SportUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Login(LoginModel model) { if (ModelState.IsValid) { SportUser user = await _userManager.FindByNameAsync(model.Name); if (user != null) { await _signInManager.SignOutAsync(); if ((await _signInManager.PasswordSignInAsync(user, model.Password, false, false)).Succeeded) { if (await _userManager.IsInRoleAsync(user, IdentityRoleNames.Employees)) { return(Redirect("/User/Index")); } else { _session.SetJson(SessionData.CustomerId, user.CustomerId); return(RedirectToAction("Index", "Customer", new { customerId = user.CustomerId })); } } } } ModelState.AddModelError("", "Nieprawidłowa nazwa użytkownika lub hasło"); return(View(model)); }
public async Task <IActionResult> List(BaseListViewModel model) { if (model == null || string.IsNullOrEmpty(model.SearchData)) { return(await List()); } else { var avaibleRoles = _roleManager.Roles.Select(r => r.Name).ToList(); avaibleRoles.Add("Wszystkie"); UserListViewModel userModel = new UserListViewModel(avaibleRoles, 1, _pageSize, 1); SportUser user = null; if (userModel.SearchData.Contains("@")) { user = await _userManager.FindByEmailAsync(model.SearchData); } else { user = await _userManager.FindByNameAsync(model.SearchData); } userModel.UsersWithRoles.Add(user, await _userManager.GetRolesAsync(user)); return(View(userModel)); } }
public IViewComponentResult Invoke(SportUser user) { Dictionary <IdentityRole, bool> componentModel = new Dictionary <IdentityRole, bool>(); foreach (var role in _roleManager.Roles) { componentModel.Add(role, _userManager.IsInRoleAsync(user, role.Name).Result); } return(View(componentModel)); }
public async Task <IActionResult> AddToRole(EditRoleViewModel model) { SportUser user = null; if (model.SearchedUserData.Contains("@")) { user = await _userManager.FindByEmailAsync(model.SearchedUserData); } else { user = await _userManager.FindByNameAsync(model.SearchedUserData); } if (user == null) { ModelState.AddModelError("", "Nie znaleziono użytkownika"); } var addToRoleResult = await _userManager.AddToRoleAsync(user, model.Role.Name); if (addToRoleResult.Errors.Any()) { AddErrors(addToRoleResult.Errors); } var roleClaims = await _roleManager.GetClaimsAsync(model.Role); foreach (var roleClaim in roleClaims) { var addClaimResult = await _userManager.AddClaimAsync(user, roleClaim); if (addClaimResult.Errors.Any()) { AddErrors(addClaimResult.Errors); } } return(RedirectToAction("EditRole", "Role", new { roleName = model.Role.Name })); }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Manage")); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new SportUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public static async Task PopulateIdentity(IApplicationBuilder app) { UserManager <SportUser> userManager = app.ApplicationServices.GetRequiredService <UserManager <SportUser> >(); RoleManager <IdentityRole> roleManager = app.ApplicationServices.GetRequiredService <RoleManager <IdentityRole> >(); IPermissionRepository permissionRepository = app.ApplicationServices.GetRequiredService <IPermissionRepository>(); var adminRole = roleManager.Roles.FirstOrDefault(r => r.Name == IdentityRoleNames.Admins); var employeeRole = roleManager.Roles.FirstOrDefault(r => r.Name == IdentityRoleNames.Employees); var clientRole = roleManager.Roles.FirstOrDefault(r => r.Name == IdentityRoleNames.Clients); var adminUser = userManager.Users.FirstOrDefault(u => u.UserName == "admin"); if (adminRole == null) { adminRole = new IdentityRole(IdentityRoleNames.Admins); await roleManager.CreateAsync(adminRole); } if (employeeRole == null) { employeeRole = new IdentityRole(IdentityRoleNames.Employees); await roleManager.CreateAsync(employeeRole); } if (clientRole == null) { clientRole = new IdentityRole(IdentityRoleNames.Clients); await roleManager.CreateAsync(clientRole); } if (adminUser == null) { adminUser = new SportUser("admin") { Email = "*****@*****.**", FirstName = "Admin" }; await userManager.CreateAsync(adminUser, "Admin1!"); } if (!await userManager.IsInRoleAsync(adminUser, adminRole.Name)) { await userManager.AddToRoleAsync(adminUser, adminRole.Name); } if (!await userManager.IsInRoleAsync(adminUser, employeeRole.Name)) { await userManager.AddToRoleAsync(adminUser, employeeRole.Name); } var adminRoleClaims = await roleManager.GetClaimsAsync(adminRole); var adminUserClaims = await userManager.GetClaimsAsync(adminUser); var securityPermissions = permissionRepository.Permissions.ToList(); foreach (Permission permission in securityPermissions) { if (!adminRoleClaims.Any(c => c.Value == permission.Value)) { Claim newClaim = new Claim(ClaimTypes.AuthenticationMethod, permission.Value, ClaimValueTypes.String); await roleManager.AddClaimAsync(adminRole, newClaim); } if (!adminUserClaims.Any(c => c.Value == permission.Value)) { Claim newClaim = new Claim(ClaimTypes.AuthenticationMethod, permission.Value, ClaimValueTypes.String); await userManager.AddClaimAsync(adminUser, newClaim); } } }