SHARE
TWEET

Untitled

a guest Aug 18th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class IdentityExtension
  2.     {
  3.         public static void AddIdentity(this IServiceCollection services, IConfiguration configuration)
  4.         {
  5.             services.Configure<IdentitySettings>(options => configuration.GetSection("IdentitySettings").Bind(options));
  6.             IdentitySettings identitySettings = configuration.GetSection(nameof(IdentitySettings)).Get<IdentitySettings>();            
  7.  
  8.             services.AddMvc(options => {
  9.                 var policy = new AuthorizationPolicyBuilder()
  10.                     .RequireAuthenticatedUser()
  11.                     .Build();
  12.                 options.Filters.Add(new AuthorizeFilter(policy));
  13.             }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  14.  
  15.             services.AddIdentity<Trader, IdentityRole<int>>(options => {
  16.                 // Password settings.
  17.                 options.Password.RequireDigit = identitySettings.PasswordRequireDigit;
  18.                 options.Password.RequireLowercase = identitySettings.PasswordRequireLowercase;
  19.                 options.Password.RequireNonAlphanumeric = identitySettings.PasswordRequireNonAlphanumic;
  20.                 options.Password.RequireUppercase = identitySettings.PasswordRequireUppercase;
  21.                 options.Password.RequiredLength = identitySettings.PasswordRequiredLength;
  22.                 options.Password.RequiredUniqueChars = 1;
  23.  
  24.                 // Lockout settings.
  25.                 options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
  26.                 options.Lockout.MaxFailedAccessAttempts = 5;
  27.                 options.Lockout.AllowedForNewUsers = true;
  28.  
  29.                 // User settings.
  30.                 options.User.AllowedUserNameCharacters =
  31.                     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
  32.                 options.User.RequireUniqueEmail = true;
  33.  
  34.                 // Set emailed token for both
  35.                 options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider;
  36.                 options.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider;                
  37.             })
  38.             .AddEntityFrameworkStores<JwtauthContext>()
  39.             .AddDefaultTokenProviders();
  40.  
  41.             services.AddAuthorization(options =>
  42.             {
  43.                 options.AddPolicy("AllTraders", builder => builder.RequireAuthenticatedUser().Build());
  44.                 options.AddPolicy("SkilledTraders", policy =>
  45.                     policy.Requirements.Add(new MinimumExperienceRequirement(1)));
  46.                 options.AddPolicy("TraderManagers", policy =>
  47.                     policy.Requirements.Add(new MinimumExperienceRequirement(5)));
  48.             });
  49.  
  50.             services.AddSingleton<IAuthorizationHandler, ExperiencedTraderHandler>();
  51.  
  52.             services.Configure<SendGridSettings>(options => configuration.GetSection("SendGridSettings").Bind(options));
  53.             services.AddTransient<IEmailSender, EmailSender>();
  54.         }
  55.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top