实现最简单的认证,类似之前的FormAuthentication
在 Startup 的 ConfigureServices() 方法中添加 Authentication 的配置: 这个CookieAuthenticationDefaults类默认的登录地址是/Account/Login,如果要要修改
则可以在后面的AddCookie()方法里修改路径
services.AddAuthentication(options => { options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; }).AddCookie();
在 Startup 的 Configure() 方法
app.UseAuthentication();
AccountController方法
public class AccountController : Controller { private readonly CRMContext _context; public AccountController(CRMContext context) { _context = context; } [AllowAnonymous] [HttpGet] public IActionResult Login() { return View(); } public async Task<IActionResult> Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return RedirectToAction("Login"); } [AllowAnonymous] [HttpPost] public async Task<IActionResult> Login(IFormCollection form) { string userName = form["txtLoginId"]; string pwd = form["txtPwd"]; if (0 == new UserLogic(_context).UserLogin(userName, pwd)) { var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, userName) }, "Basic"); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal); return Json(new { isSuccess = true, message = "登录成功" }); } else { return Json(new { isSuccess = false, message = "登录失败" }); } }
我们之前只能把登录的用户名放在FormAuthentication的cookie里, 使用时就用User.Identity.Name获得当前登录的用户名,
但是现在我们可以把其他的信息,如UserId,SystemId都放到ClaimsIdentity里. 这样写
var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, userName), new Claim(ClaimTypes.Sid, "1"), new Claim(ClaimTypes.System,"HR") }, "Basic"); //使用方法 //User.Claims.FirstOrDefault(t => t.Type == System.Security.Claims.ClaimTypes.Sid).Value //User.Claims.FirstOrDefault(t => t.Type == System.Security.Claims.ClaimTypes.System).Value
登录提交Form的参数, 要改成IFormCollection,否则会出错
The 'Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormCollectionModelBinder' cannot bind to a model of type 'Microsoft.AspNetCore.Http.FormCollection'.
Change the model type to 'Microsoft.AspNetCore.Http.IFormCollection' instead.
System.Security.Cryptography.HashAlgorithm.Create(string hashName) Hash方法还没实现,会出现错误
解决方法, 要添加nuget包
https://stackoverflow.com/questions/35363358/computing-sha1-with-asp-net-core
这样写
var sha1 = System.Security.Cryptography.SHA1.Create();
参考文章:
https://www.cnblogs.com/seriawei/p/7452743.html
http://www.cnblogs.com/dudu/p/7631927.html
http://www.cnblogs.com/dudu/p/6368240.html
http://www.cnblogs.com/bidianqing/p/6870163.html
http://www.cnblogs.com/tdfblog/p/aspnet-core-security-authentication-cookie.html
http://www.cnblogs.com/RainingNight/p/introduce-basic-authentication-in-asp-net-core.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?