Advertisement
hunostor

Custom Identity (Scaffold)

Dec 16th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. // https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2&tabs=visual-studio
  2. public void ConfigureServices(IServiceCollection services)
  3. {
  4.     services.Configure<CookiePolicyOptions>(options =>
  5.     {
  6.         options.CheckConsentNeeded = context => true;
  7.         options.MinimumSameSitePolicy = SameSiteMode.None;
  8.     });
  9.  
  10.     services.AddDbContext<ApplicationDbContext>(options =>
  11.         options.UseSqlServer(
  12.             Configuration.GetConnectionString("DefaultConnection")));
  13.  
  14.     services.AddIdentity<IdentityUser, IdentityRole>()
  15.         // services.AddDefaultIdentity<IdentityUser>()
  16.         .AddEntityFrameworkStores<ApplicationDbContext>()
  17.         .AddDefaultTokenProviders();
  18.  
  19.     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
  20.         .AddRazorPagesOptions(options =>
  21.         {
  22.             options.AllowAreas = true;
  23.             options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
  24.             options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
  25.         });
  26.  
  27.     services.ConfigureApplicationCookie(options =>
  28.     {
  29.         options.LoginPath = $"/Identity/Account/Login";
  30.         options.LogoutPath = $"/Identity/Account/Logout";
  31.         options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
  32.     });
  33.  
  34.     // using Microsoft.AspNetCore.Identity.UI.Services;
  35.     services.AddSingleton<IEmailSender, EmailSender>();
  36. }
  37.  
  38. // create EmailSender service
  39. public class EmailSender : IEmailSender
  40. {
  41.     public Task SendEmailAsync(string email, string subject, string message)
  42.     {
  43.         return Task.CompletedTask;
  44.     }
  45. }
  46.  
  47. // and scaffold identity razor templates
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement