Identity in .netCore RazorProject

10/22/2020 MVC
1254

احراز هویت در پروژه ها ی تحت وب خودش ماجراهایی دارد و روشها و متدهای مختلفی رایج ترین آنها استفاده  از Cookie است که با استفاده از روشهای مختلفی میتوان ان را به پروژه اضافه کرد از آنجایی که پروژه های من عمومآ از نوع RazorPage Projects هستند برای اضافه کردن از این روشها استفاده میکنم 

add

 services.AddDbContext<SnContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
            );

to get Connectio string

rewrite appsettings.json -add connectionsstrinf- 

 be careful about "s" end of ConnectionString 

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=123.123.123.123;Database=saas;Integrated Security=false;Initial Catalog=saas;User ID=****;Password=C***;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

add this code to configur Identity

   services.AddRazorPages();

            services.AddIdentity<ApplicationUser, IdentityRole>(options =>
            {
                options.Password.RequireDigit = true;
                options.Password.RequireLowercase = true;
                options.Password.RequireUppercase = true;
                options.Password.RequiredLength = 6;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequiredUniqueChars = 6;

                // Lockout settings.
                options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
                options.Lockout.MaxFailedAccessAttempts = 5;
                options.Lockout.AllowedForNewUsers = true;

                // User settings.
                options.User.AllowedUserNameCharacters =
                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
                options.User.RequireUniqueEmail = true;

            })
             .AddEntityFrameworkStores<SnContext>()
             .AddDefaultTokenProviders();

            services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.HttpOnly = true;
                options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

                options.LoginPath = "/Identity/Account/Login";
                options.AccessDeniedPath = "/Identity/Account/AccessDenied";
                options.SlidingExpiration = true;
            });

 

add to AuthorizeAdminFolder

 services.AddMvc()
                    .AddRazorPagesOptions(options =>
                    {
                        //options.Conventions.AuthorizeFolder("/Account/Manage");
                        //options.Conventions.AuthorizePage("/Account/Logout");
                        options.Conventions.AuthorizeAreaFolder("admin", "/ProjectsPages");
                    });

add this to configur in strartup.cs

  app.UseAuthentication();
            app.UseAuthorization();

add to projectContext in modelProject

public class ApplicationUser : IdentityUser
    {
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        [Display(Name = "Date of birth")]
        public DateTime BirthDate { get; set; }
    }
    public class SnContext :IdentityDbContext<IdentityUser>
    {
        public SnContext(DbContextOptions<SnContext> option):base (option){}
}