zoukankan      html  css  js  c++  java
  • 44- EF + Identity实现

    1-配置EF, 需要创建如下几个类

     默认User主键为guid类型,现在改成int类型

    namespace MvcCookieAuthSample.Models
    {
        public class ApplicationUser:IdentityUser<int>
        {
    
        }
    namespace MvcCookieAuthSample.Models
    {
        public class ApplicationUserRole:IdentityRole<int>
        {
      
        }
    }
    namespace MvcCookieAuthSample.Data
    {
        public class ApplicationDbContext:IdentityDbContext<ApplicationUser,ApplicationUserRole,int>
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options):base(options)
            {
    
            }
        }
    }

    在Startup.cs 启用EF

        public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<ApplicationDbContext>(options => {
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                });
            }

    在appsetting.json配置连接数据字符串

    "ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=aspnet-IdentitySample-C9275B98-9E63-4E35-AE50-5F96CA59C41D;Trusted_Connection=True;MultipleActiveResultSets=true"
      }

    使用Nuget增加   Microsoft.EntityFrameworkCore.Tools

    然后在控制台运行相关EF命令生成数据库表,  http://www.siyouku.cn/article/6871.html

    E:coding
    etcoreMvcCookieAuthSample>dotnet ef migrations add VSInit
    E:coding
    etcoreMvcCookieAuthSample>dotnet ef database update
    

      

    2-启用验证

            public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<ApplicationDbContext>(options=> {
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                });
    
    
                services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                    .AddCookie(options=>{//自定义登陆地址,不配置的话则默认为http://localhost:5000/Account/Login
                        options.LoginPath="/Account/Login";
                    });
    
                services.Configure<CookiePolicyOptions>(options =>
                {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    options.CheckConsentNeeded = context => true;
                    options.MinimumSameSitePolicy = SameSiteMode.None;
                });
    
                services.AddIdentity<ApplicationUser, ApplicationUserRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();
    
                services.Configure<IdentityOptions>(option=> {
                    option.Password.RequireLowercase = false;
                    option.Password.RequireNonAlphanumeric = false;
                    option.Password.RequireUppercase = false;
                });
    
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            }

    注册账号

      public class AccountController : Controller
        {
            private UserManager<Models.ApplicationUser> _userManager;
            private SignInManager<Models.ApplicationUser> _signInManager;
            public AccountController(UserManager<Models.ApplicationUser> userManager,
                SignInManager<Models.ApplicationUser> signInManager)
            {
                _signInManager = signInManager;
                _userManager = userManager;
            }
    
            [HttpPost]
            public async Task<IActionResult> Register(ViewModel.RegisterViewModel registerViewModel)
            {
                Models.ApplicationUser applicationUser = new Models.ApplicationUser()
                {
                    Email = registerViewModel.Email,
                    UserName = registerViewModel.Email,
                    NormalizedEmail = registerViewModel.Email
                };
    
                IdentityResult result = await _userManager.CreateAsync(applicationUser, registerViewModel.Password);
                if (result.Succeeded)
                {
                    return RedirectToAction("Index", "Home");
                }
                return View();
            }
    
    
        }
  • 相关阅读:
    boltdb 学习和实践
    解读-m addrtype --dst-type LOCAL -j DOCKER
    找到linux中当前java的安装位置
    java.library.path是什么?
    Linux 中【./】和【/】和【.】之间有什么区别?
    [SpringMVC]JSON乱码问题
    iOS MVVM的理解与分析
    iOS中为什么必须在主线程操作UI
    几种直播流媒体协议
    IOS硬编码
  • 原文地址:https://www.cnblogs.com/qinzb/p/9398861.html
Copyright © 2011-2022 走看看