使用ClaimsIdentity来实现登录授权

背景:以前做登录时用的都是FormsAuthentication.SetAuthCookie(model.UID, IsRemeber),但是有一个不好,不能存储多个值,有时候我们既想存储登录用户的UID又想存储用户名,以前都是将两者拼接成字符串,用的时候在split出来,比较麻烦,现在用ClaimsIdentity就很方便。

1、登录时验证通过存储

  ClaimsIdentity ci = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
                    ci.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, model.UserName));
                    ci.AddClaim(new Claim(ClaimTypes.NameIdentifier, model.UID));
                    ci.AddClaim(new Claim("HspUID", model.HspUID));
                    AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = IsRemeber }, ci);

需要用到下面的

 private IAuthenticationManager AuthenticationManager
        {
          
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用Furion+sqlsugar实现单用户登录的步骤: 1. 创建一个Furion项目,并添加sqlsugar和sqlsugar.extensions库的引用。 2. 在appsettings.json文件中添加数据库连接字符串配置,例如: ``` { "ConnectionStrings": { "DefaultConnection": "Server=localhost;Database=TestDb;Uid=root;Pwd=123456;" } } ``` 3. 创建一个User表,用于存储用户信息,例如: ``` CREATE TABLE `User` ( `Id` bigint(20) NOT NULL AUTO_INCREMENT, `UserName` varchar(50) NOT NULL, `Password` varchar(50) NOT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ``` 4. 在Startup.cs文件的ConfigureServices方法中添加sqlsugar的配置,例如: ``` services.AddSqlSugar(new ConnectionConfig() { ConnectionString = Configuration.GetConnectionString("DefaultConnection"), DbType = DbType.MySql, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); ``` 5. 在UserService.cs文件中定义一个用于验证用户登录的方法,例如: ``` public class UserService : IUserService { private readonly ISqlSugarClient _sqlSugarClient; public UserService(ISqlSugarClient sqlSugarClient) { _sqlSugarClient = sqlSugarClient; } public async Task<bool> ValidateUserAsync(string userName, string password) { var user = await _sqlSugarClient.Queryable<User>().Where(u => u.UserName == userName && u.Password == password).FirstAsync(); return user != null; } } ``` 6. 在AccountController.cs文件中定义一个用于处理用户登录的方法,例如: ``` public class AccountController : ApiControllerBase { private readonly IUserService _userService; public AccountController(IUserService userService) { _userService = userService; } [HttpPost] public async Task<ActionResult> LoginAsync(LoginInputDto input) { if (await _userService.ValidateUserAsync(input.UserName, input.Password)) { var identity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, input.UserName), }, CookieAuthenticationDefaults.AuthenticationScheme); var principal = new ClaimsPrincipal(identity); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); return Ok(new { message = "登录成功!" }); } else { return BadRequest("用户名或密码错误!"); } } [HttpGet] public async Task<ActionResult> LogoutAsync() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return Ok(new { message = "退出登录成功!" }); } } ``` 7. 在Configure方法中添加Cookie身份验证中间件的配置,例如: ``` app.UseAuthentication(); app.UseAuthorization(); ``` 8. 在appsettings.json文件中添加身份验证配置,例如: ``` { "Authentication": { "Cookie": { "LoginPath": "/Account/Login", "LogoutPath": "/Account/Logout", "AccessDeniedPath": "/Home/AccessDenied", "Expiration": "1.00:00:00", "SlidingExpiration": true } } } ``` 9. 编写前端页面,使用ajax请求登录接口,登录成功后跳转到主页面。 以上就是使用Furion+sqlsugar实现单用户登录的全部步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值