Guest User

Untitled

a guest
Oct 28th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. using Biblioteka.DAL;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using Microsoft.AspNet.Identity;
  9. using Microsoft.AspNet.Identity.EntityFramework;
  10. using Biblioteka.Models;
  11.  
  12. namespace Biblioteka.App_Start
  13. {
  14. public class IdentityDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
  15. {
  16. protected override void Seed(ApplicationDbContext context)
  17. {
  18. var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
  19. var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
  20. var myinfo = new MyUserInfo() { FirstName = "Jan", LastName = "Kowalski" };
  21. string roleName = "Admin";
  22. string password = "admin";
  23.  
  24. UserManager.Create(new ApplicationUser() { UserName = "Kazio", UserInfo = myinfo }, password);
  25.  
  26. if (!RoleManager.RoleExists(roleName))
  27. {
  28. var roleresult = RoleManager.Create(new IdentityRole(roleName));
  29. }
  30.  
  31. var user = new ApplicationUser();
  32. user.UserName = "admin";
  33. user.UserInfo = myinfo;
  34. var createResult = UserManager.Create(user, password);
  35.  
  36.  
  37.  
  38. if (createResult.Succeeded)
  39. {
  40. var result = UserManager.AddToRole(user.UserName, roleName);
  41. }
  42.  
  43. base.Seed(context);
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment