Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Identity;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using CSGO.Data;
- using CSGO.Models;
- using CSGO.Services;
- namespace CSGO {
- public class Startup {
- public Startup(IConfiguration configuration) {
- Configuration = configuration;
- }
- private async Task CreateRoles(IServiceProvider serviceProvider) {
- var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
- var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
- string[] roleNames = { "Administrator", "User" };
- IdentityResult roleResult;
- foreach (var roleName in roleNames) {
- var roleExist = await RoleManager.RoleExistsAsync(roleName);
- if (!roleExist) {
- roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
- }
- }
- var admin = new ApplicationUser { UserName = "admin@admin.com", Email = "admin@admin.com" };
- string password = "@dministrat0R";
- var _user = await UserManager.FindByEmailAsync("admin@admin.com");
- if (_user == null) {
- var createPowerUser = await UserManager.CreateAsync(admin, password);
- if (createPowerUser.Succeeded) {
- await UserManager.AddToRoleAsync(admin, "Administrator");
- }
- }
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services) {
- services.AddDbContext<ApplicationDbContext>(options =>
- options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
- services.AddIdentity<ApplicationUser, IdentityRole>()
- .AddEntityFrameworkStores<ApplicationDbContext>()
- .AddDefaultTokenProviders();
- // Add application services.
- services.AddTransient<IEmailSender, EmailSender>();
- services.AddMvc();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider) {
- if (env.IsDevelopment()) {
- app.UseDeveloperExceptionPage();
- app.UseBrowserLink();
- app.UseDatabaseErrorPage();
- } else {
- app.UseExceptionHandler("/Home/Error");
- }
- app.UseStaticFiles();
- app.UseAuthentication();
- app.UseMvc(routes => {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
- CreateRoles(serviceProvider).Wait();
- }
- }
- }
Add Comment
Please, Sign In to add comment