Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. private async Task CreateUserRoles(IServiceProvider serviceProvider) {
  2.  
  3.  
  4. var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole<int>>>();
  5. var userManager = serviceProvider.GetRequiredService<UserManager<User>>();
  6.  
  7. var roleName = "Super Admin";
  8. var roleCheck = await roleManager.RoleExistsAsync(roleName);
  9. if (!roleCheck) {
  10. Role role = new Role();
  11. role.Name = roleName;
  12. IdentityResult result = roleManager.CreateAsync(role).Result;
  13. //IdentityResult roleResult = await roleManager.CreateAsync(new IdentityRole<int>(roleName));
  14. }
  15. User user = new User();
  16. user.UserName = "someone";
  17. user.Password = "someone";
  18. user.Email = "someone@gmail.com";
  19. ApplicationDbContext context = new ApplicationDbContext();
  20. context.Users.Add(user);
  21. context.SaveChanges();
  22. user = await userManager.FindByEmailAsync("someone@gmail.com");
  23.  
  24. await userManager.AddToRoleAsync(user, roleName);
  25. }
  26.  
  27. public class Startup {
  28. public Startup(IConfiguration configuration) {
  29. Configuration = configuration;
  30. }
  31.  
  32. public IConfiguration Configuration { get; }
  33.  
  34. // This method gets called by the runtime. Use this method to add services to the container.
  35. public void ConfigureServices(IServiceCollection services) {
  36. services.Configure<CookiePolicyOptions>(options => {
  37. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  38. options.CheckConsentNeeded = context => true;
  39. options.MinimumSameSitePolicy = SameSiteMode.None;
  40. });
  41.  
  42. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  43. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  44.  
  45.  
  46. // Add MVC services to the services container.
  47. services.AddMvc();
  48. services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache
  49. services.AddSession(opt => { opt.Cookie.IsEssential = true; });
  50.  
  51. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
  52. .AddRazorPagesOptions(options => {
  53. options.AllowAreas = true;
  54. options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Settings");
  55. options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
  56. });
  57. services.AddDbContext<ApplicationDbContext>(options =>
  58. options.UseNpgsql("User ID = postgres; Password = sa; Host = localhost; Port = 5432; Database = CCN; Pooling = true;"));
  59.  
  60. services.ConfigureApplicationCookie(options => {
  61. options.LoginPath = $"/Account/Login"; //options.LoginPath = $"/Identity/Account/Login";
  62. options.LogoutPath = $"/Account/Logout";
  63. options.AccessDeniedPath = $"/Account/AccessDenied";
  64. });
  65.  
  66.  
  67.  
  68. //Password settings
  69. services.AddIdentity<User, Role>(o => {
  70. o.Password.RequireDigit = false;
  71. o.Password.RequireLowercase = false;
  72. o.Password.RequireUppercase = false;
  73. o.Password.RequireNonAlphanumeric = false;
  74. //o.Password.RequiredLength = 3;
  75.  
  76. }).AddEntityFrameworkStores<ApplicationDbContext>().AddRoles<IdentityRole>()
  77. .AddDefaultTokenProviders();
  78.  
  79. }
  80.  
  81. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  82. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider services) {
  83.  
  84. app.UseDeveloperExceptionPage();
  85. app.UseStatusCodePages();
  86. if (env.IsDevelopment()) {
  87. app.UseDeveloperExceptionPage();
  88. }
  89. else {
  90. app.UseExceptionHandler("/Home/Index");
  91. app.UseHsts();
  92. }
  93.  
  94. app.UseHttpsRedirection();
  95. app.UseStaticFiles();
  96. app.UseCookiePolicy();
  97. //Enable session
  98. app.UseSession();
  99.  
  100.  
  101. app.UseAuthentication();
  102.  
  103. app.UseMvc(routes => {
  104. routes.MapRoute(
  105. name: "default",
  106. template: "{controller=Home}/{action=Index}");
  107. });
  108.  
  109. //Create user role and assign it
  110. CreateUserRoles(services).Wait();
  111.  
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement