예제 #1
0
 public CreateRuoliModel(
     ApplicationDbContext db,
     Microsoft.AspNetCore.Identity.UserManager <Models.Utenti> userManager,
     Microsoft.AspNetCore.Identity.RoleManager <Models.Ruoli> roleManager)
 {
     _db          = db;
     _userManager = userManager;
     _roleManager = roleManager;
 }
예제 #2
0
        internal static void Initialize(AppDbContext context, Microsoft.AspNetCore.Identity.RoleManager <Microsoft.AspNetCore.Identity.IdentityRole> roleManager, Microsoft.AspNetCore.Identity.UserManager <Microsoft.AspNetCore.Identity.IdentityUser> userManager)
        {
            if (!roleManager.RoleExistsAsync("Admin").Result)
            {
                IdentityRole role = new IdentityRole("Admin");

                roleManager.CreateAsync(role).Wait();
            }

            if (!roleManager.RoleExistsAsync("NormalUser").Result)
            {
                IdentityRole role = new IdentityRole("NormalUser");

                roleManager.CreateAsync(role).Wait();
            }

            //-------------------------------------------------------------------

            if (userManager.FindByNameAsync("Guru").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";

                IdentityResult result = userManager.CreateAsync(user, "Password!123").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "Admin").Wait();
                }
            }

            if (userManager.FindByNameAsync("Sven").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";

                IdentityResult result = userManager.CreateAsync(user, "Password!123").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "NormalUser").Wait();
                }
            }

            //-----------------------------------------------------------------------

            // Don´t forget to save
            //context.SaveChanges();
        }
예제 #3
0
        internal static void Initialize(DbContext context, Microsoft.AspNetCore.Identity.RoleManager <Microsoft.AspNetCore.Identity.IdentityRole> roleManager, Microsoft.AspNetCore.Identity.UserManager <Microsoft.AspNetCore.Identity.IdentityUser> userManager)
        {
            if (!roleManager.RoleExistsAsync("Admin").Result)
            {
                IdentityRole role = new IdentityRole("Admin");

                roleManager.CreateAsync(role).Wait();
            }

            if (!roleManager.RoleExistsAsync("NormalUser").Result)
            {
                IdentityRole role = new IdentityRole("NormalUser");

                roleManager.CreateAsync(role).Wait();
            }

            //------------------------------------------------------------------------------------//

            if (userManager.FindByNameAsync("Abduallah").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";

                IdentityResult result = userManager.CreateAsync(user, "Abduallah1996-").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "Admin").Wait();
                }
            }

            if (userManager.FindByNameAsync("Abdullah").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";


                var result = userManager.CreateAsync(user, "Abduallah1996-");

                if (result.IsCompletedSuccessfully)
                {
                    userManager.AddToRoleAsync(user, "NormalUser").Wait();
                }
            }
            //------------------------------------------------------------------------------------------

            //Glöm inte att spara
            //context.SaveChanges();
        }
예제 #4
0
        public static void SeedData(Microsoft.AspNetCore.Identity.RoleManager <IdentityRole> roleManager)
        {
            if (!roleManager.RoleExistsAsync(RoleConstants.Administrator).Result)
            {
                var adminRole = new IdentityRole()
                {
                    Name = RoleConstants.Administrator
                };
                roleManager.CreateAsync(adminRole).Wait();
            }

            if (!roleManager.RoleExistsAsync(RoleConstants.Visitor).Result)
            {
                var visitorRole = new IdentityRole()
                {
                    Name = RoleConstants.Visitor
                };
                roleManager.CreateAsync(visitorRole).Wait();
            }
        }
예제 #5
0
 public IndexRuoliModel(ApplicationDbContext db, Microsoft.AspNetCore.Identity.RoleManager <Models.Ruoli> roleManager)
 {
     _db          = db;
     _roleManager = roleManager;
 }
예제 #6
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        //public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        //{
        //    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        //    loggerFactory.AddDebug();

        //    app.UseMvc();
        //}

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
                              Microsoft.AspNetCore.Identity.RoleManager <IdentityRole> roleManager)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            var signingKey = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes(secretKey));

            var jwtOptions = new TokenProviderOptions
            {
                Audience           = audience,
                Issuer             = issure,
                SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
                Expiration         = TimeSpan.FromDays(1)
            };

            app.UseMiddleware <TokenProviderMiddleware>(Options.Create(jwtOptions));

            var tokenValidationParameters = new TokenValidationParameters
            {
                //The signing key must match !
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = signingKey,

                //Validate the JWT Issuer (iss) claim
                ValidateIssuer = true,
                ValidIssuer    = issure,

                //validate the JWT Audience (aud) claim

                ValidateAudience = true,
                ValidAudience    = audience,

                //validate the token expiry
                ValidateLifetime = true,

                // If you  want to allow a certain amout of clock drift
                ClockSkew = TimeSpan.Zero
            };

            app.UseJwtBearerAuthentication(new JwtBearerOptions
            {
                AutomaticAuthenticate     = false,
                AutomaticChallenge        = true,
                TokenValidationParameters = tokenValidationParameters,
                AuthenticationScheme      = "Bearer"
            });

            app.UseIdentity();

            // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
            //Data.DbInitializer.Initialize();
        }
예제 #7
0
 public User(Microsoft.AspNetCore.Identity.RoleManager <Microsoft.AspNetCore.Identity.IdentityRole> roleManager) : base()
 {
     this.roleManager = roleManager;
 }
        internal void Seed(Microsoft.AspNetCore.Identity.UserManager <ApplicationUser> userManager, Microsoft.AspNetCore.Identity.RoleManager <Microsoft.AspNetCore.Identity.IdentityRole> roleManager)
        {
            if (!Users.Any(u => u.UserName == "test123"))
            {
                var user = new ApplicationUser
                {
                    UserName           = "******",
                    NormalizedUserName = "******",
                    Email           = "*****@*****.**",
                    NormalizedEmail = "*****@*****.**",
                    EmailConfirmed  = true
                };

                user.PasswordHash = userManager.PasswordHasher.HashPassword(user, "Pa$$w0rd");

                Users.Add(user);
                SaveChanges();
            }
        }
예제 #9
0
        /// <summary>
        /// Create the default application roles
        /// </summary>
        /// <param name="userMgr"></param>
        /// <param name="roleMgr"></param>
        /// <returns></returns>
        public async Task <bool> EnsureSeedData(Microsoft.AspNetCore.Identity.UserManager <ApplicationUser> userMgr, Microsoft.AspNetCore.Identity.RoleManager <IdentityRole> roleMgr)
        {
            var adminRole = await roleMgr.FindByNameAsync("Admin");

            if (adminRole == null)
            {
                adminRole = new IdentityRole("Admin");
                await roleMgr.CreateAsync(adminRole);
            }

            var userRole = await roleMgr.FindByNameAsync("User");

            if (userRole == null)
            {
                userRole = new IdentityRole("User");
                await roleMgr.CreateAsync(userRole);
            }
            return(true);
        }