Guest User

Untitled

a guest
Jan 17th, 2018
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Identity;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.AspNetCore.Hosting;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using CSGO.Data;
  12. using CSGO.Models;
  13. using CSGO.Services;
  14.  
  15. namespace CSGO {
  16. public class Startup {
  17. public Startup(IConfiguration configuration) {
  18. Configuration = configuration;
  19. }
  20.  
  21. private async Task CreateRoles(IServiceProvider serviceProvider) {
  22. var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
  23. var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
  24. string[] roleNames = { "Administrator", "User" };
  25. IdentityResult roleResult;
  26.  
  27. foreach (var roleName in roleNames) {
  28. var roleExist = await RoleManager.RoleExistsAsync(roleName);
  29. if (!roleExist) {
  30. roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
  31. }
  32. }
  33.  
  34. var admin = new ApplicationUser { UserName = "admin@admin.com", Email = "admin@admin.com" };
  35. string password = "@dministrat0R";
  36. var _user = await UserManager.FindByEmailAsync("admin@admin.com");
  37.  
  38. if (_user == null) {
  39. var createPowerUser = await UserManager.CreateAsync(admin, password);
  40. if (createPowerUser.Succeeded) {
  41. await UserManager.AddToRoleAsync(admin, "Administrator");
  42. }
  43. }
  44. }
  45.  
  46. public IConfiguration Configuration { get; }
  47.  
  48. // This method gets called by the runtime. Use this method to add services to the container.
  49. public void ConfigureServices(IServiceCollection services) {
  50. services.AddDbContext<ApplicationDbContext>(options =>
  51. options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  52.  
  53. services.AddIdentity<ApplicationUser, IdentityRole>()
  54. .AddEntityFrameworkStores<ApplicationDbContext>()
  55. .AddDefaultTokenProviders();
  56.  
  57. // Add application services.
  58. services.AddTransient<IEmailSender, EmailSender>();
  59.  
  60. services.AddMvc();
  61. }
  62.  
  63. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  64. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider) {
  65. if (env.IsDevelopment()) {
  66. app.UseDeveloperExceptionPage();
  67. app.UseBrowserLink();
  68. app.UseDatabaseErrorPage();
  69. } else {
  70. app.UseExceptionHandler("/Home/Error");
  71. }
  72.  
  73. app.UseStaticFiles();
  74.  
  75. app.UseAuthentication();
  76.  
  77. app.UseMvc(routes => {
  78. routes.MapRoute(
  79. name: "default",
  80. template: "{controller=Home}/{action=Index}/{id?}");
  81. });
  82. CreateRoles(serviceProvider).Wait();
  83. }
  84. }
  85. }
Add Comment
Please, Sign In to add comment