24-集成ASP.NETCore Identity采用EF

1-增加IdentityServer4.AspNetIdentity nuget包

2- StartUp.cs启用增加相应的代码  .AddAspNetIdentity<ApplicationUser>()

  public void ConfigureServices(IServiceCollection services)
        {

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

            services.AddIdentity<ApplicationUser, ApplicationUserRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();


            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddAspNetIdentity<ApplicationUser>()
            ;

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddScoped<Services.ConsentServices>();
        }

3-去掉之前注释过的代码

  CreateWebHostBuilder(args)
                
                .Build()
                .MigrationDbContext<ApplicationDbContext>((context, services) =>
                {
                    new Data.ApplicationDbContextSeed().SeedAsync(context, services).Wait();
                })
                .Run();

3-修改Lgoin中的代码,不再使用之前测试的代码

依赖注入的代码

        public AccountController(UserManager<Models.ApplicationUser> userManager,
            SignInManager<Models.ApplicationUser> signInManager,
            IIdentityServerInteractionService identityServerInteractionService)
        {
            _signInManager = signInManager;
            _userManager = userManager;
            _identityServerInteractionService = identityServerInteractionService;
        }

        private UserManager<Models.ApplicationUser> _userManager;
        private SignInManager<Models.ApplicationUser> _signInManager;
        private IIdentityServerInteractionService _identityServerInteractionService;
   [HttpPost]
        public async Task<IActionResult> Login(ViewModel.LoginViewModel loginModel, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                var findUser = await _userManager.FindByEmailAsync(loginModel.Email);
                //  string returnUrl = Request.Form["returnUrl"];
                if (findUser == null)
                {
                    ModelState.AddModelError(nameof(loginModel.Email), "用户不存在");
                }
                else
                {
                    if (await _userManager.CheckPasswordAsync(findUser, loginModel.Password))
                    {
                        AuthenticationProperties properties = null;
                        if (loginModel.RememberMe)
                        {
                            properties = new AuthenticationProperties()
                            {
                                IsPersistent = true,
                                ExpiresUtc = System.DateTimeOffset.UtcNow.Add(TimeSpan.FromMinutes(30))
                            };
                        }

                        await _signInManager.SignInAsync(findUser, properties);
                        if (_identityServerInteractionService.IsValidReturnUrl(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        return Redirect("~/");
                    }
                    ModelState.AddModelError(nameof(loginModel.Password), "密码不正确");
                }
                return View();
            }
            else
            {
                return View();
            }
           

        }

猜你喜欢

转载自www.cnblogs.com/qinzb/p/9581550.html
今日推荐