public async Task<ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles) { if (ModelState.IsValid) { var user = new AspNetUser { UserName = userViewModel.Email, Email = userViewModel.Email }; var adminresult = await UserManager.CreateAsync(user, userViewModel.Password); //Add User to the selected Roles if (adminresult.Succeeded) { if (selectedRoles != null) { var result = await UserManager.AddToRolesAsync(user.Id, selectedRoles); if (!result.Succeeded) { ModelState.AddModelError("", result.Errors.First()); ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name"); return View(); } } } else { ModelState.AddModelError("", adminresult.Errors.First()); ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name"); return View(); } return RedirectToAction("Index"); } ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name"); return View(); }
private async Task SaveAccessToken(TMD.Models.DomainModels.AspNetUser user, ClaimsIdentity identity) { var userclaims = await UserManager.GetClaimsAsync(user.Id); foreach (var at in ( from claims in identity.Claims where claims.Type.EndsWith("access_token") select new Claim(claims.Type, claims.Value, claims.ValueType, claims.Issuer))) { if (!userclaims.Contains(at)) { await UserManager.AddClaimAsync(user.Id, at); } } }
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 AspNetUser { 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 async Task<ActionResult> SignUp(AspNetUserModel oModel) { oModel.UserName = oModel.Email; Utility oUtility = new Utility(); oModel.RoleId = Utility.MemberRoleId; oModel.RoleName = Utility.MemberRoleName; var user = new AspNetUser { UserName = oModel.UserName, Email = oModel.Email, Address = oModel.Address, Telephone = oModel.Telephone, FirstName = oModel.FirstName, LastName = oModel.LastName, LockoutEnabled = false }; user.EmailConfirmed = true; if (!String.IsNullOrEmpty(oModel.Password)) { var result = await UserManager.CreateAsync(user, oModel.Password); if (result.Succeeded) { //Setting role var roleManager = HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); var roleName = roleManager.FindById(oModel.RoleId).Name; UserManager.AddToRole(user.Id, roleName); return PreparePayPalPayment(user); // Add User Preferences for Dashboards Widgets //TempData["message"] = new MessageViewModel { Message = "Employee has been created", IsSaved = true }; //return RedirectToAction("Users"); } } return View(oModel); }
public async Task<ActionResult> Subscribe(SignupViewModel signupViewModel) { // Add new User // Check if User already exists var usernames = AspNetUserService.GetAllUsers().Select(x => x.UserName); if (usernames.Contains(signupViewModel.UserName)) { // it means username is already taken TempData["message"] = new MessageViewModel { Message = TMD.Web.Resources.HR.Account.EmpError, IsError = true }; return View(signupViewModel); } var user = new AspNetUser { UserName = signupViewModel.UserName, Email = signupViewModel.Email }; user.EmailConfirmed = true; if (!String.IsNullOrEmpty(signupViewModel.Password)) { var result = await UserManager.CreateAsync(user, signupViewModel.Password); if (result.Succeeded) { return RedirectToAction("Index","Home"); } } return View(signupViewModel); }
public async Task<ActionResult> Create(AspNetUsersViewModel model) { model.AspNetUserModel.UserName = model.AspNetUserModel.Email; #region Update if (!string.IsNullOrEmpty(model.AspNetUserModel.Id)) { //Means Update // Get role var roleName = RoleManager.FindById(model.AspNetUserModel.RoleId).Name; AspNetUser userResult = UserManager.FindById(model.AspNetUserModel.Id); string userrRoleID = userResult.AspNetRoles.ToList()[0].Id; string userRoleName = RoleManager.FindById(userrRoleID).Name; // Check if role has been changed /************** DISABLING CHANGE ROLE IMPLEMENTATION/ UNCOMMENT TO RUN if (userrRoleID != model.AspNetUserModel.RoleId) { // Update User Role UserManager.RemoveFromRole(model.AspNetUserModel.Id, userRoleName); UserManager.AddToRole(model.AspNetUserModel.Id, roleName); TempData["message"] = new MessageViewModel { Message = "Role has been updated", IsUpdated = true }; }************************/ // Password Reset if (!String.IsNullOrEmpty(model.AspNetUserModel.Password)) { var token = await UserManager.GeneratePasswordResetTokenAsync(model.AspNetUserModel.Id); var resetPwdResults = await UserManager.ResetPasswordAsync(model.AspNetUserModel.Id, token, model.AspNetUserModel.Password); if (resetPwdResults.Succeeded) { var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); if (user != null) { await SignInAsync(user, isPersistent: false); } TempData["message"] = new MessageViewModel { Message = TMD.Web.Resources.HR.Account.UpdatePass, IsUpdated = true }; } } // Get user by UserId to Update User AspNetUser userToUpdate = UserManager.FindById(model.AspNetUserModel.Id); //if (userToUpdate.Email != model.AspNetUserModel.Email) //{ if (userToUpdate != null) { userToUpdate.UpdateUserTo(model.AspNetUserModel); } var updateUserResult = await UserManager.UpdateAsync(userToUpdate); if (updateUserResult.Succeeded) { TempData["message"] = new MessageViewModel { Message = "User has been Updated", IsUpdated = true }; } //} return RedirectToAction("Users"); } #endregion // Add new User if (ModelState.IsValid) { // TODO:Check # of Users that Admin can create var user = new AspNetUser { UserName = model.AspNetUserModel.UserName, Email = model.AspNetUserModel.Email, Address = model.AspNetUserModel.Address, Telephone = model.AspNetUserModel.Telephone,FirstName = model.AspNetUserModel.FirstName,LastName = model.AspNetUserModel.LastName, LockoutEnabled = false, RegisterPayPalDate = null }; user.EmailConfirmed = true; if (!String.IsNullOrEmpty(model.AspNetUserModel.Password)) { var result = await UserManager.CreateAsync(user, model.AspNetUserModel.Password); if (result.Succeeded) { //Setting role var roleManager = HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); var roleName = roleManager.FindById(model.AspNetUserModel.RoleId).Name; UserManager.AddToRole(user.Id, roleName); // Add User Preferences for Dashboards Widgets TempData["message"] = new MessageViewModel { Message = "Employee has been created", IsSaved = true }; return RedirectToAction("Users"); } else { var resultStr = ""; if (result.Errors.Count() > 0) resultStr = result.Errors.ToList()[0].ToString(); TempData["message"] = new MessageViewModel { Message = resultStr, IsError = true }; ViewBag.MessageVM = TempData["message"] as MessageViewModel; } } } // If we got this far, something failed, redisplay form model.Roles = HttpContext.GetOwinContext().Get<ApplicationRoleManager>().Roles.ToList(); //TempData["message"] = new MessageViewModel { Message = TMD.Web.Resources.HR.Account.ChkFields, IsError = true }; return View(model); }
private async Task SignInAsync(AspNetUser user, bool isPersistent) { //AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie); //AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager)); //AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); //var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); //await SetExternalProperties(identity); //AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); //await SaveAccessToken(user, identity); }
private void updateSessionValues(AspNetUser user) { AspNetUser result = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(User.Identity.GetUserId()); string role = HttpContext.GetOwinContext().Get<ApplicationRoleManager>().FindById(result.AspNetRoles.ToList()[0].Id).Name; //Session["FullName"] = result.Employee.EmployeeFirstName + " " + result.Employee.EmployeeLastName; Session["UserID"] = result.Id; Session["RoleName"] = role; }
private ActionResult PreparePayPalPayment(AspNetUser oModel) { string token = string.Empty; string retMsg = string.Empty; NVPAPICaller PPCaller = new NVPAPICaller(); bool ret = PPCaller.ExpressCheckout(oModel.FirstName +" "+oModel.LastName +" Basic Product", "BASIC", "220", "1", "USD", oModel.Email, ref token, ref retMsg); if (ret) { Session["token"] = token; return Redirect(retMsg); } else { //PayPal has not responded successfully, let user know //lblError.Text = "PayPal is not responding, please try again in a few moments."; return RedirectToAction("Index", "Home"); } //string IP = ConfigurationManager.AppSettings["PayPalBaseUrl"]; //string businessPaypalId = ConfigurationManager.AppSettings["BusinessPayPalId"]; //string businessPaypalTransction = ConfigurationManager.AppSettings["PayPalTxnUrl"]; //double itemCost = 10.00; //string redirect2 = IP + @"Account/Thankyou"; //string IPN = IP + @"Account/PayPalIPN"; //string Cancel = IP + @"Home/Index"; //string redirect = businessPaypalTransction+"&business=" + businessPaypalId; //redirect += "&amount=" + itemCost; //redirect += "&custom=" + oModel.Email; //redirect += "&address1=" + oModel.Address; //redirect += "&email=" + oModel.Email; //redirect += "&item_number=1"; //redirect += "¤cy_code= USD"; //redirect += "&return=" + redirect2; //redirect += "&cancel_return=" + Cancel; //redirect += "&item_name=" + "Basic Package"; //redirect += "¬ify_url=" + IPN; //return Redirect(redirect); }