Ejemplo n.º 1
0
        public static void ConfigureIdentity(this IServiceCollection services)
        {
            var builder = services.AddIdentityCore <ApiUser>(q => q.User.RequireUniqueEmail = true);

            builder = new Microsoft.AspNetCore.Identity.IdentityBuilder(builder.UserType, typeof(IdentityRole), services);
            builder.AddEntityFrameworkStores <DatabaseContext>().AddDefaultTokenProviders();
        }
        private static void BuildIdentity(IServiceCollection services)
        {
            var identityBuilder = services.AddIdentityCore <AppUser>(identityOption =>
            {
                identityOption.Password.RequireDigit           = false;
                identityOption.Password.RequireLowercase       = false;
                identityOption.Password.RequireUppercase       = false;
                identityOption.Password.RequireNonAlphanumeric = false;
                identityOption.Password.RequiredLength         = 6;
            });

            identityBuilder = new Microsoft.AspNetCore.Identity.IdentityBuilder(identityBuilder.UserType, typeof(IdentityRole), identityBuilder.Services);
            identityBuilder.AddEntityFrameworkStores <UserContext>().AddDefaultTokenProviders();
        }
Ejemplo n.º 3
0
        public static void ConfigureIdentity(this IServiceCollection services)
        {
            var builder = services.AddIdentityCore <User>(u =>
            {
                u.Password.RequireDigit           = true;
                u.Password.RequiredLength         = 8;
                u.Password.RequireLowercase       = false;
                u.Password.RequireUppercase       = true;
                u.Password.RequireNonAlphanumeric = true;
                u.User.RequireUniqueEmail         = true;
            }
                                                          );

            builder = new Microsoft.AspNetCore.Identity.IdentityBuilder(builder.UserType, typeof(IdentityRole), builder.Services);
            builder.AddEntityFrameworkStores <RepositoryContext>().AddDefaultTokenProviders();
        }
        public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration config)
        {
            var builder = services.AddIdentityCore <AppUser>();

            builder = new Microsoft.AspNetCore.Identity.IdentityBuilder(builder.UserType, builder.Services);
            builder.AddEntityFrameworkStores <AppIdentityDbContext>();
            builder.AddUserManager <UserManager <AppUser> >();
            builder.AddSignInManager <SignInManager <AppUser> >();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options => {
                options.TokenValidationParameters = new TokenValidationParameters {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Token:Key"])),
                    ValidIssuer      = config["Token:Issuer"],
                    ValidateIssuer   = true,
                    ValidateAudience = false
                };
            });
            return(services);
        }
Ejemplo n.º 5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <DatingDbContext>((options) => {
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionString"))
                .EnableSensitiveDataLogging();
            });

            // services.AddDbContext<DatingDbContext>((options) => {
            //     options.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionString"));
            // });

            var identityBuilder = services.AddIdentityCore <User>(options => {
                options.Password.RequireDigit           = false;
                options.Password.RequiredLength         = 4;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;
            });

            identityBuilder = new Microsoft.AspNetCore.Identity.IdentityBuilder(identityBuilder.UserType, typeof(Role), services);

            identityBuilder.AddEntityFrameworkStores <DatingDbContext>();
            identityBuilder.AddRoleValidator <RoleValidator <Role> >();
            identityBuilder.AddRoleManager <RoleManager <Role> >();
            identityBuilder.AddSignInManager <SignInManager <User> >();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options => {
                var securityKey        = Configuration.GetSection("AuthenticationSettings:SecretKey").Value;
                var securityKeyInBytes = System.Text.Encoding.UTF8.GetBytes(securityKey);
                var IssuerSigningKey   = new SymmetricSecurityKey(securityKeyInBytes);

                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuerSigningKey = true,
                    ValidateAudience         = false,
                    ValidateIssuer           = false,
                    IssuerSigningKey         = IssuerSigningKey
                };
            });

            services.AddAuthorization(options => {
                options.AddPolicy("AdminPolicy", policy => policy.RequireRole("Admin"));
                options.AddPolicy("MemberPolicy", policy => policy.RequireRole("Member"));
                options.AddPolicy("ModeratorPolicy", policy => policy.RequireRole("Admin", "Moderator"));
            });

            services.AddControllers(options => {
                var policy = new AuthorizationPolicyBuilder()
                             .RequireAuthenticatedUser()
                             .Build();

                options.Filters.Add(new AuthorizeFilter(policy));
            });
            services.AddControllers().AddNewtonsoftJson(setup => {
                setup.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });
            services.AddScoped <IAuthorization, Authorization>();
            services.AddScoped <IUserRepository, userRepository>();
            services.AddScoped <IPhotoRepository, PhotoRepository>();
            services.AddScoped <ILikeRepository, LikeRepository>();
            services.AddScoped <ImessageRepository, MessageRepository>();
            services.AddScoped <LogUserActivity>();
            services.Configure <CloudinarySettings>(this.Configuration.GetSection("CloudinarySettings"));
            services.Configure <AuthenticationSettings>(this.Configuration.GetSection("AuthenticationSettings"));

            services.AddAutoMapper(typeof(userRepository).Assembly);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Registers <see cref="NonCommonPasswordValidator"/> as a password validator along with two <see cref="IPasswordBlacklistProvider"/>, the <see cref="DefaultPasswordBlacklistProvider"/>
 /// and <see cref="ConfigPasswordBlacklistProvider"/>, using <see cref="User"/> class as a user type.
 /// </summary>
 /// <param name="builder">Helper functions for configuring identity services.</param>
 /// <returns>The <see cref="IdentityBuilder"/>.</returns>
 public static IdentityBuilder AddNonCommonPasswordValidator(this IdentityBuilder builder) => builder.AddNonCommonPasswordValidator <User>();
Ejemplo n.º 7
0
 public static IdentityBuilder AddYubikeyOTPTokenProvider(this IdentityBuilder builder)
 {
     return(builder.AddYubikeyOTPTokenProvider(setupAction: null));
 }
 private static void AddSignInManagerDeps(this IdentityBuilder builder)
 {
     builder.Services.AddHttpContextAccessor();
     builder.Services.AddScoped(typeof(ISecurityStampValidator), typeof(SecurityStampValidator <>).MakeGenericType(builder.UserType));
     builder.Services.AddScoped(typeof(ITwoFactorSecurityStampValidator), typeof(TwoFactorSecurityStampValidator <>).MakeGenericType(builder.UserType));
 }
Ejemplo n.º 9
0
        private static void AddRelatedParts(IdentityBuilder builder, UIFramework framework)
        {
            var mvcBuilder = builder.Services
                             .AddMvc()
                             .ConfigureApplicationPartManager(partManager =>
            {
                var thisAssembly      = typeof(IdentityBuilderUIExtensions).Assembly;
                var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(thisAssembly, throwOnError: true);
                var relatedParts      = relatedAssemblies.ToDictionary(
                    ra => ra,
                    CompiledRazorAssemblyApplicationPartFactory.GetDefaultApplicationParts);

                var selectedFrameworkAssembly = _assemblyMap[framework];

                foreach (var kvp in relatedParts)
                {
                    var assemblyName = kvp.Key.GetName().Name;
                    if (!IsAssemblyForFramework(selectedFrameworkAssembly, assemblyName))
                    {
                        RemoveParts(partManager, kvp.Value);
                    }
                    else
                    {
                        AddParts(partManager, kvp.Value);
                    }
                }

                bool IsAssemblyForFramework(string frameworkAssembly, string assemblyName) =>
                string.Equals(assemblyName, frameworkAssembly, StringComparison.OrdinalIgnoreCase);

                void RemoveParts(
                    ApplicationPartManager manager,
                    IEnumerable <ApplicationPart> partsToRemove)
                {
                    for (var i = 0; i < manager.ApplicationParts.Count; i++)
                    {
                        var part = manager.ApplicationParts[i];
                        if (partsToRemove.Any(p => string.Equals(
                                                  p.Name,
                                                  part.Name,
                                                  StringComparison.OrdinalIgnoreCase)))
                        {
                            manager.ApplicationParts.Remove(part);
                        }
                    }
                }

                void AddParts(
                    ApplicationPartManager manager,
                    IEnumerable <ApplicationPart> partsToAdd)
                {
                    foreach (var part in partsToAdd)
                    {
                        if (!manager.ApplicationParts.Any(p => p.GetType() == part.GetType() &&
                                                          string.Equals(p.Name, part.Name, StringComparison.OrdinalIgnoreCase)))
                        {
                            manager.ApplicationParts.Add(part);
                        }
                    }
                }
            });
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Adds a default, self-contained UI for Identity to the application using
 /// Razor Pages in an area named Identity.
 /// </summary>
 /// <remarks>
 /// In order to use the default UI, the application must be using <see cref="Microsoft.AspNetCore.Mvc"/>,
 /// <see cref="Microsoft.AspNetCore.StaticFiles"/> and contain a <c>_LoginPartial</c> partial view that
 /// can be found by the application.
 /// </remarks>
 /// <param name="builder">The <see cref="IdentityBuilder"/>.</param>
 /// <returns>The <see cref="IdentityBuilder"/>.</returns>
 public static IdentityBuilder AddDefaultUI(this IdentityBuilder builder) => builder.AddDefaultUI(UIFramework.Bootstrap4);