Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Login(LoginModel model, string returnUrl)
- {
- ApplicationUser user = await UserManager.FindAsync(model.Email, model.Password);
- if (user == null)
- {
- ModelState.AddModelError("", "Failed login or pass");
- }else
- {
- ClaimsIdentity claim = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
- AuthenticationManager.SignOut();
- AuthenticationManager.SignIn(new AuthenticationProperties
- {
- IsPersistent = true
- } , claim);
- if(User.IsInRole("admin"))
- {
- RedirectToAction("Adminka", "Home");
- }else if(User.IsInRole("user"))
- {
- RedirectToAction("Index", "Home");
- }
- if (String.IsNullOrEmpty(returnUrl))
- return RedirectToAction("Index", "Home");
- return Redirect(returnUrl);
- }
- ViewBag.returnUrl = returnUrl;
- return View(model);
- }
- public ActionResult Logout()
- {
- AuthenticationManager.SignOut();
- return RedirectToAction("Login");
- }
- public class AppDbInitializer : DropCreateDatabaseAlways<ApplicationContext>
- {
- protected override void Seed(ApplicationContext context)
- {
- var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
- var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
- var roleAdmin = new IdentityRole { Name = "admin" };
- var roleUser = new IdentityRole { Name = "user" };
- roleManager.Create(roleAdmin);
- roleManager.Create(roleUser);
- var admin = new ApplicationUser { Email = "somemail@mail.ru", UserName = "somemail@mail.ru" };
- string password = "12345678";
- var result = userManager.Create(admin, password);
- if(result.Succeeded)
- {
- userManager.AddToRole(admin.Id, roleAdmin.Name);
- userManager.AddToRole(admin.Id, roleUser.Name);
- }
- base.Seed(context);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement