private void InitUser(Appreciator user) { this.Id = user.Id; this.DrinkingName = user.DrinkingName; this.UserName = user.UserName; this.DrinkingClubs = user.DrinkingClubs?.Select(dc => new DrinkingClubDto(dc)).ToList(); }
public async Task <JsonResult> RegisterAsync(RegisterViewModel model) { var user = new Appreciator() { UserName = model.Email, Email = model.Email, DrinkingName = model.DrinkingName }; IdentityResult result = await this.UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var currentUser = this.UserManager.FindByName(model.Email); this.UserManager.AddToRole(currentUser.Id, RoleNames.Member); await SignInAsync(user, isPersistent : false); // For more information on how to enable account confirmation and password reset please visit http://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(new JsonResult { Data = new { Success = true, Data = user } }); } return(new JsonResult { Data = new { Success = false, Error = string.Join(", ", result.Errors) } }); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new Appreciator() { UserName = model.Email, Email = model.Email, DrinkingName = model.DrinkingName }; IdentityResult result = await this.UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var currentUser = this.UserManager.FindByName(model.Email); this.UserManager.AddToRole(currentUser.Id, RoleNames.Member); await SignInAsync(user, isPersistent : false); // For more information on how to enable account confirmation and password reset please visit http://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")); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return(View(model)); }
/// <summary> /// Updates the specified Appreciator /// </summary> /// <param name="id">The Appreciator identifier.</param> /// <param name="appreciator">The appreciator.</param> /// <param name="updateGraph">if set to <c>true</c> any child objects of the entity will also be persisted.</param> /// <returns> /// A HttpResponseMessage containing the result of the PUT /// </returns> /// <remarks> /// PUT: api/appreciators/{id} /// </remarks> public HttpResponseMessage Put(string id, Appreciator appreciator, bool updateGraph = false) { this.appreciatorService.UpdateAppreciator(id, appreciator, updateGraph); // Success return(new HttpResponseMessage(HttpStatusCode.OK)); }
private async Task SignInAsync(Appreciator user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager)); }
private static string GetUserRoleString(Appreciator user) { if (user == null || !user.Roles.Any()) { return(string.Empty); } return(string.Join("|", user.Roles.Select(r => r.RoleId))); }
/// <summary> /// Adds a new Appreciator /// </summary> /// <param name="appreciator">The Appreciator.</param> /// <param name="updateGraph">if set to <c>true</c> any child objects of the entity will also be persisted.</param> /// <returns> /// A HttpResponseMessage containing the result of the POST /// </returns> /// <remarks> /// POST: api/Appreciators /// </remarks> public HttpResponseMessage Post(Appreciator appreciator, bool updateGraph = false) { string appreciatorId = this.appreciatorService.InsertAppreciator(appreciator, updateGraph); // Success, return a uri to the created resource in the response header var response = new HttpResponseMessage(HttpStatusCode.Created); response.Headers.Location = new Uri(this.Request.RequestUri.AbsoluteUri + "/" + appreciatorId.ToString()); return(response); }
/// <summary> /// Creates the user claims. /// </summary> /// <param name="user">The user.</param> /// <param name="authenticationType">Type of the authentication.</param> /// <param name="scopes">The scope requested by the client (i.e. what the token will be able to access, e.g. InViron Service).</param> /// <returns> /// The user claims /// </returns> private static ClaimsIdentity CreateUserClaims(Appreciator user, string authenticationType, IList <string> scopes) { IList <Claim> claims = new List <Claim> { new Claim(ClaimTypes.Name, user.UserName), new Claim("Roles", GetUserRoleString(user)) }; AddScopesToClaims(claims, scopes); return(new ClaimsIdentity(claims, authenticationType)); }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("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 Appreciator { UserName = model.Email, Email = model.Email, DrinkingName = model.DrinkingName }; IdentityResult result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent : false); // For more information on how to enable account confirmation and password reset please visit http://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); // SendEmail(user.Email, callbackUrl, "Confirm your account", "Please confirm your account by clicking this link"); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
private void InitRoles(Appreciator user, List <IdentityRole> securityRoles) { var userRoleIds = user.Roles.Select(r => r.RoleId).ToList(); if (!userRoleIds.Any()) { return; } this.Roles = securityRoles .Where(sr => userRoleIds.Contains(sr.Id)) .Select(sr => new SecurityRoleDto(sr)).ToList(); foreach (var role in securityRoles) { if (userRoleIds.Contains(role.Id)) { } } }
private static void CreateDefaultAdmin(UserManager <Appreciator, string> userManager) { var admin = userManager.FindByNameAsync(DefaultAdminEmail).Result; if (admin != null) { return; } var adminUser = new Appreciator() { UserName = DefaultAdminEmail, DrinkingName = "UberDrunk", Email = DefaultAdminEmail }; var userResult = userManager.Create(adminUser, DefaultAdminPassword); if (userResult.Succeeded) { userManager.AddToRole(adminUser.Id, RoleNames.Admin); } }
/// <summary> /// Updates the specified Appreciator. /// </summary> /// <param name="id">The identifier.</param> /// <param name="appreciator">The Appreciator.</param> /// <param name="updateGraph">if set to <c>true</c> any child objects of the entity will also be persisted.</param> public void UpdateAppreciator(string id, Appreciator appreciator, bool updateGraph) { using (var unitOfWork = this.unitOfWorkFactory.Create()) { // Get the entity repository IEntityRepository <Appreciator> entityRepository = unitOfWork.GetRepository <Appreciator, string>(); // Update the entity if (updateGraph) { entityRepository.InsertOrUpdateGraph(appreciator); } else { entityRepository.Update(appreciator); } // Persist the changes unitOfWork.Save(); } }
/// <summary> /// Adds the Appreciator to the database. /// </summary> /// <param name="appreciator">The Appreciator.</param> /// <param name="updateGraph">if set to <c>true</c> any child objects of the entity will also be persisted.</param> /// <returns>The identifier of the appreciator added to database</returns> public string InsertAppreciator(Appreciator appreciator, bool updateGraph) { using (var unitOfWork = this.unitOfWorkFactory.Create()) { // Get the entity repository IEntityRepository <Appreciator> entityRepository = unitOfWork.GetRepository <Appreciator, string>(); // Insert the entity if (updateGraph) { // Update any entities in the graph entityRepository.InsertOrUpdateGraph(appreciator); } else { // Update just the root entity entityRepository.Insert(appreciator); } // Persist the changes and return Id return(unitOfWork.Save().GetInsertedEntityKey <string>("Appreciators")); } }
public AppreciatorDto(Appreciator user, List <IdentityRole> securityRoles) { this.InitUser(user); this.InitRoles(user, securityRoles); }