private async Task SignInAsync(ApplicationUser user) { bool isPersistent = false; AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); }
public async Task<ActionResult> Register(RegisterViewModel model) { try { if (_userManager.FindByName(model.UserName) == null) { var group = _repository.Find<Group>(Guid.Parse(model.GroupVal)); var user = new ApplicationUser { UserName = model.UserName.ToLower(), LastLogin = DateTime.Now, OsysDateTime = DateTime.Now, EditDateTime=DateTime.Now, Maker = User.Identity.GetUserName(), FirstName = model.Firstname, LastName = model.Lastname, Enabled=MyEnums.Active.Disable, Status=MyEnums.StatusOptions.Added, GroupId = group.Id, AlternateNumber = model.AlternateNumber, PhoneNumber = model.PhoneNumber, Country=model.Country, Company = model.Company, Email = model.Email, IdentificationNumber = model.IdentificationNumber }; _userManager.Create(user, user.UserName.ToLower() + "1234"); var fetchRoles = _repository.Fetch<MenuMap>() .Where(p => p.GroupId == user.GroupId && p.Status == MyEnums.StatusOptions.Approved).DistinctBy(p => p.MenuItems.ParentName); foreach (var n in fetchRoles) { if (!_userManager.IsInRole(user.Id, n.MenuItems.ParentName)) { _userManager.AddToRole(user.Id, n.MenuItems.ParentName); } } _getVals.LogAudit(User.Identity.GetUserName(), "Created", Request.UserHostName, "Created User: "******"Fullnames: "+user.FirstName+user.LastName+"Phone number: "+user.PhoneNumber+"Alternate Phone number: "+user.AlternateNumber+"Email: "+user.Enabled+"Country: "+user.Country+"Company: "+user.Country+" with Group " + group.Name+"at"+user.OsysDateTime, "ISA", "UserManagement"); return RedirectToAction("AccountIndex"); } else { ModelState.AddModelError("", "User Already Exist"); } return RedirectToAction("AccountIndex"); } catch (Exception ex) { _logs.LogError(User.Identity.GetUserName(), "RegisterUser", "Error: " + ex.Message, Request.ServerVariables["REMOTE_ADDR"], HttpContext.Server.MapPath("."), "ISA"); System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/errorLOG.txt"), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite); System.IO.StreamWriter s = new System.IO.StreamWriter(fs); s.BaseStream.Seek(0, System.IO.SeekOrigin.End); s.WriteLine("ERROR DATE: " + System.DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture) + " \nERROR MESSAGE: " + ex.Message + "\nSOURCE: " + ex.Source + "\nFORM NAME: " + System.Web.HttpContext.Current.Request.Url.ToString() + "\nQUERYSTRING: " + Request.QueryString.ToString() + "\nTARGETSITE: " + ex.TargetSite.ToString() + "\nSTACKTRACE: " + ex.StackTrace + System.Diagnostics.EventLogEntryType.Error); s.WriteLine("-------------------------------------------------------------------------------------------------------------"); s.Close(); return RedirectToAction("AccountIndex"); } }
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 ApplicationUser() { UserName = model.UserName }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }