Identity1(学习笔记12)
1、新建DbContext 继承IdentityDbContext
1 2 3 4 5 6 7 8 9 10 | namespace WebApplication34 { public class MyDbContext:IdentityDbContext { public MyDbContext(DbContextOptions<MyDbContext> options ): base ( options) { } } } |
2、配置startup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get ; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<MyDbContext>(option=> { option.UseSqlServer(Configuration.GetConnectionString( "SqlServer" )); }); services.AddIdentity<IdentityUser, IdentityRole>(config => { config.User.RequireUniqueEmail = false ; config.Lockout.AllowedForNewUsers = false ; }) .AddEntityFrameworkStores<MyDbContext>(); services.ConfigureApplicationCookie(config => { config.Cookie.Name = "Identity.Cooke" ; config.LoginPath = "/Home/Login" ; config.AccessDeniedPath = "/Home/Deny" ; }); services.AddControllersWithViews(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler( "/Home/Error" ); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default" , pattern: "{controller=Home}/{action=Index}/{id?}" ); }); } } |
3、 数据迁移 add-migration update-database
4、 用户管理 (微软提供了 UserManager、SignInManager)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | namespace WebApplication34.Controllers { public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private readonly SignInManager<IdentityUser> _signInManager; private readonly UserManager<IdentityUser> _userManager; public HomeController( ILogger<HomeController> logger, UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager) { _logger = logger; _signInManager = signInManager; _userManager = userManager; } public IActionResult Index() { return View(); } [Authorize(Roles = "admin" )] public IActionResult Privacy() { return View(); } [Authorize] [HttpGet] public async Task<IActionResult> Users() { var users=await _userManager.Users.ToListAsync(); return View(users); } [HttpPost] public async Task<ActionResult> SignIn( string username, string password) { var user = await _userManager.FindByNameAsync(username); var signInResult = await _signInManager.PasswordSignInAsync(user, password, false , false ); if (signInResult.Succeeded) { return RedirectToAction( "Index" ); } return RedirectToAction( "Login" ); } [HttpGet] public async Task<IActionResult> EditUser( string id) { var user = await _userManager.FindByIdAsync(id); if (user == null ) { return RedirectToAction( "Index" ); ; } var claims = await _userManager.GetClaimsAsync(user); var vm = new UserEditViewModel { Id = user.Id, Email=user.Email, UserName=user.UserName, Claims=claims.Select(x=>x.Value).ToList() }; return View(vm); } [Authorize] [HttpPost] public async Task<IActionResult> DeleteUser( string id) { var user = await _userManager.FindByIdAsync(id); if (user != null ) { var result = await _userManager.DeleteAsync(user); if (result.Succeeded) { return RedirectToAction( "Users" ); } } else { ModelState.AddModelError( string .Empty, "用户找不到" ); } return View( "Users" , await _userManager.Users.ToListAsync()); } [HttpGet] public IActionResult Login() { return View(); } [HttpPost] public async Task<IActionResult> Logout() { await _signInManager.SignOutAsync(); return RedirectToAction( "Index" ); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现