Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. private void CreateAdminIfNeeded()
  2. {
  3. // Get Admin Account
  4. string AdminUserName = ConfigurationManager.AppSettings["AdminUserName"];
  5. string AdminPassword = ConfigurationManager.AppSettings["AdminPassword"];
  6. string fileName = HttpContext.Server.MapPath(@"~/Images/noImg.png");
  7.  
  8. byte[] imageData = null;
  9. FileInfo fileInfo = new FileInfo(fileName);
  10. long imageFileLength = fileInfo.Length;
  11. FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  12. BinaryReader br = new BinaryReader(fs);
  13. imageData = br.ReadBytes((int)imageFileLength);
  14. // See if Admin exists
  15. var objAdminUser = UserManager.FindByEmail(AdminUserName);
  16.  
  17. if (objAdminUser == null)
  18. {
  19. //See if the Admin role exists. In this part I am getting error
  20. if (!RoleManager.RoleExists("Administrator"))
  21. {
  22. // Create the Admin Role (if needed)
  23. IdentityRole objAdminRole = new IdentityRole("Administrator");
  24. RoleManager.Create(objAdminRole);
  25. }
  26.  
  27. // Create Admin user
  28. var objNewAdminUser = new ApplicationUser { UserName = AdminUserName, Email = AdminUserName, UserPhoto = imageData };
  29. var AdminUserCreateResult = UserManager.Create(objNewAdminUser, AdminPassword);
  30. // Put user in Admin role
  31. UserManager.AddToRole(objNewAdminUser.Id, "Administrator");
  32. }
  33. }
  34. #endregion
  35.  
  36. //See if the Admin role exists.
  37. if (!RoleManager.RoleExists("Administrator"))
  38. {
  39. // Create the Admin Role (if needed)
  40. IdentityRole objAdminRole = new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement