• 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();
                }
               
    
            }
  • 相关阅读:
    javascript 回调函数定义 模板
    获得最近一天的提交,并使用winscp上传到服务器
    virltualbox 升级之后 苹果虚拟机报The installed support driver doesn't match the version of the user解决方案
    ESP-EYE V2.1 开发板 WINDOWS 10 开发入门
    centos 安装gitee备忘
    Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br
    requirejs amd module load example
    js object template
    php 基础代码大全(不断完善中)
    自动化web前端测试,自动登录网站.目前发现最靠谱的方法是imacros
  • 原文地址:https://www.cnblogs.com/qinzb/p/9581550.html
Copyright © 2020-2023  润新知