Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public async Task<ActionResult> Login(LoginModel model, string returnUrl)
  4. {
  5. ApplicationUser user = await UserManager.FindAsync(model.Email, model.Password);
  6.  
  7.  
  8.  
  9. if (user == null)
  10. {
  11. ModelState.AddModelError("", "Failed login or pass");
  12. }else
  13. {
  14. ClaimsIdentity claim = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
  15. AuthenticationManager.SignOut();
  16. AuthenticationManager.SignIn(new AuthenticationProperties
  17. {
  18. IsPersistent = true
  19. } , claim);
  20.  
  21. if(User.IsInRole("admin"))
  22. {
  23. RedirectToAction("Adminka", "Home");
  24. }else if(User.IsInRole("user"))
  25. {
  26. RedirectToAction("Index", "Home");
  27. }
  28.  
  29.  
  30. if (String.IsNullOrEmpty(returnUrl))
  31.  
  32. return RedirectToAction("Index", "Home");
  33.  
  34. return Redirect(returnUrl);
  35.  
  36.  
  37.  
  38. }
  39.  
  40.  
  41. ViewBag.returnUrl = returnUrl;
  42. return View(model);
  43. }
  44.  
  45.  
  46.  
  47. public ActionResult Logout()
  48. {
  49. AuthenticationManager.SignOut();
  50. return RedirectToAction("Login");
  51. }
  52.  
  53. public class AppDbInitializer : DropCreateDatabaseAlways<ApplicationContext>
  54. {
  55.  
  56. protected override void Seed(ApplicationContext context)
  57. {
  58. var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
  59. var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
  60.  
  61.  
  62. var roleAdmin = new IdentityRole { Name = "admin" };
  63. var roleUser = new IdentityRole { Name = "user" };
  64.  
  65. roleManager.Create(roleAdmin);
  66. roleManager.Create(roleUser);
  67.  
  68.  
  69.  
  70.  
  71. var admin = new ApplicationUser { Email = "somemail@mail.ru", UserName = "somemail@mail.ru" };
  72. string password = "12345678";
  73.  
  74.  
  75. var result = userManager.Create(admin, password);
  76.  
  77.  
  78.  
  79. if(result.Succeeded)
  80. {
  81. userManager.AddToRole(admin.Id, roleAdmin.Name);
  82. userManager.AddToRole(admin.Id, roleUser.Name);
  83. }
  84.  
  85.  
  86. base.Seed(context);
  87. }
  88.  
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement