zoukankan      html  css  js  c++  java
  • 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();
                }
               
    
            }
  • 相关阅读:
    最小生成树——prim
    最短路径——floyd(多源最短路径)
    最短路径——Dijkstra(简易版)
    图的遍历——BFS(队列实现)
    图的遍历——DFS(邻接矩阵)
    图的创建——十字链表
    图的创建——邻接表法
    图的创建——邻接矩阵
    队列——链表实现
    队列——数组实现(循环队列)
  • 原文地址:https://www.cnblogs.com/qinzb/p/9581550.html
Copyright © 2011-2022 走看看