private static void SeedDatabase() { var context = new OnlineShopContext(); var userStore = new UserStore<ApplicationUser>(context); var userManager = new ApplicationUserManager(userStore); var user = new ApplicationUser() { UserName = "******", Email = "*****@*****.**" }; var result = userManager.CreateAsync(user, "Test123%").Result; if (!result.Succeeded) { Assert.Fail(string.Join(Environment.NewLine, result.Errors)); } context.Categories.Add(new Category() { Name = "Auto" }); context.Categories.Add(new Category() { Name = "Education" }); context.Categories.Add(new Category() { Name = "home and Family" }); context.AdTypes.Add(new AdType() { Name = "Economy", PricePerDay = 10 }); context.AdTypes.Add(new AdType() { Name = "Normal", PricePerDay = 20 }); context.AdTypes.Add(new AdType() { Name = "Lux", PricePerDay = 40 }); context.SaveChanges(); }
private void SetupFakeAds() { var adTypes = new List<AdType>() { new AdType() { Id = 1, Name = "Normal", Index = 100, PricePerDay = 100 }, new AdType() { Id = 2, Name = "Premium", Index = 200, PricePerDay = 100 }, }; var categories = new List<Category>() { new Category() { Id = 1, Name = "Category #1" }, new Category() { Id = 2, Name = "Category #2" } }; var owner = new ApplicationUser() { Id = "123", UserName = "******" }; var fakeAds = new List<Ad>() { new Ad() { Id = 1, Name = "Audi A6", Type = adTypes[0], PostedOn = DateTime.Now.AddDays(-6), Owner = owner, Price = 400, Categories = categories }, new Ad() { Id = 2, Name = "Bmw 320", Type = adTypes[1], PostedOn = DateTime.Now.AddDays(-3), Owner = owner, Price = 300, Categories = categories }, new Ad() { Id = 3, Name = "Lada", Type = adTypes[0], PostedOn = DateTime.Now.AddDays(-6), Owner = owner, Price = 1400, Categories = categories }, }; this.AdRepositoryMock = new Mock<IRepository<Ad>>(); this.AdRepositoryMock.Setup(r => r.All()).Returns(fakeAds.AsQueryable()); this.AdRepositoryMock.Setup(r => r.Find(It.IsAny<int>())).Returns( (int id) => { return fakeAds.FirstOrDefault(a => a.Id == id); }); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }
public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var info = await Authentication.GetExternalLoginInfoAsync(); if (info == null) { return InternalServerError(); } var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user); if (!result.Succeeded) { return GetErrorResult(result); } result = await UserManager.AddLoginAsync(user.Id, info.Login); if (!result.Succeeded) { return GetErrorResult(result); } return Ok(); }
public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var user = new ApplicationUser() { UserName = model.Username, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return GetErrorResult(result); } return Ok(); }
private static void SeedDatabase() { var context = new OnlineShopContext(); var userStore = new UserStore<ApplicationUser>(context); var userManager = new ApplicationUserManager(userStore); var user = new ApplicationUser { UserName = TestUserUsername, Email = "*****@*****.**" }; var result = userManager.CreateAsync(user, TestUserPassword).Result; if (!result.Succeeded) { Assert.Fail(string.Join(Environment.NewLine, result.Errors)); } SeedCategories(context); SeedAdTypes(context); }
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 ApplicationUser { 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> Register(RegisterViewModel model) { if (string.IsNullOrEmpty(Session["Captcha"].ToString()) || Session["Captcha"].ToString() != model.Captcha) { return View(model); } if (ModelState.IsValid && Session["Captcha"].ToString() == model.Captcha) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var 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 this link: <a href=\"" + callbackUrl + "\">link</a>"); ViewBag.Link = callbackUrl; return View("DisplayEmail"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
public async Task<ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles) { if (ModelState.IsValid) { var user = new ApplicationUser { 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(); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { 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 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"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
private async Task SignInAsync(ApplicationUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); }
private void CreateAndLoginUser() { if (!IsValid) { return; } var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var signInManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>(); var user = new ApplicationUser() { UserName = email.Text, Email = email.Text }; IdentityResult result = manager.Create(user); if (result.Succeeded) { var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(); if (loginInfo == null) { RedirectOnFail(); return; } result = manager.AddLogin(user.Id, loginInfo.Login); if (result.Succeeded) { signInManager.SignIn(user, isPersistent: false, rememberBrowser: false); // Дополнительные сведения о том, как включить подтверждение учетной записи и сброс пароля, см. по адресу: http://go.microsoft.com/fwlink/?LinkID=320771 // var code = manager.GenerateEmailConfirmationToken(user.Id); // Отправить эту ссылку по электронной почте: IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id) IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); return; } } AddErrors(result); }
private async Task SignInAsync(ApplicationUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager)); }