Configuring the application to use Identity

In order to use Identity, we just need to add the following line in the Configure method of the Startup class:

app.UseIdentity(); 

The complete Configure method is shown in the following code, along with the call of the UseIdentity method, which is app.UseIdentity():

public void Configure(IApplicationBuilder app, IHostingEnvironment env,  ILoggerFactory loggerFactory) 
        { 
            loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
            loggerFactory.AddDebug(); 
 
            if (env.IsDevelopment()) 
            { 
                app.UseBrowserLink(); 
                app.UseDeveloperExceptionPage(); 
                app.UseDatabaseErrorPage(); 
            } 
            else 
            { 
                app.UseExceptionHandler("/Home/Error"); 
 
                 
 
            app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear()); 
 
            app.UseStaticFiles(); 
 
            app.UseIdentity(); 
 
            // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715 
 
            app.UseMvc(routes => 
            { 
                routes.MapRoute( 
                    name: "default", 
                    template: "{controller=Home}/{action=Index}/{id?}"); 
            }); 
        } 

In the ConfigureServices method, we will make the following changes:

  • We will add the ApplicationDbContext class with the connection string taken from the appsettings.json file
  • We will add Identity with UserStore and RoleStore
  • Finally, we will ask ASP.NET Core to return AuthMessageSender whenever we ask for the IEmailSender and ISMSSender classes
    public void ConfigureServices(IServiceCollection services 
    { 
    // Add framework services. 
     
                services.AddScoped<ApplicationDbContext>(f => { 
                    return new ApplicationDbContext(Configuration["Data:DefaultConnection:ConnectionString"]); 
                }); 
         
                services.AddIdentity<ApplicationUser, IdentityRole>() 
                    .AddUserStore<UserStore<ApplicationUser, ApplicationDbContext>>() 
                    .AddRoleStore<RoleStore<ApplicationDbContext>>() 
                    .AddDefaultTokenProviders(); 
     
                services.AddMvc(); 
     
                // Add application services. 
                services.AddTransient<IEmailSender, AuthMessageSender>(); 
                services.AddTransient<ISmsSender, AuthMessageSender>(); 
            } 
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset