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();
                }
               
    
            }
  • 相关阅读:
    输入/输出的格式和方法
    程序编译运行和exe运行之文件位置的区别
    ZOJ_3950_How Many Nines 解题报告及如何对程序进行测试修改
    [Offer收割]编程练习赛13 解题报告
    查找语言自带函数
    codeblocks下的汇编语言
    hiho一下 第144周(机会渺茫)解题报告及拓展
    关闭调试窗口快捷方式
    编写程序一个位置的快速到达和修改
    poj3660(Cow Contest)解题报告
  • 原文地址:https://www.cnblogs.com/qinzb/p/9581550.html
Copyright © 2011-2022 走看看