(更新时间)2021年5月31日 商城高并发秒杀系统(.NET Core版) 15-通用用户模型绑定的封装
【摘要】
一:通用用户模型绑定的封装
/// <summary>
/// 系统用户(用于存储用户状态相关信息)
/// </summary>
public class SysUser
{
...
一:通用用户模型绑定的封装
/// <summary>
/// 系统用户(用于存储用户状态相关信息)
/// </summary>
public class SysUser
{
public int UserId { set; get; } // 用户Id
public string UserName { set; get; } // 用户名称
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
/// <summary>
/// 系统用户模型绑定(从头部信息获取用户信息,UserId和UserName)
/// 1、将HttpContext用户信息转换成为Sysuser
/// 2、将Sysuser绑定到action方法参数
/// </summary>
public class SysUserCookieModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
if (bindingContext.ModelType == typeof(SysUser))
{
// 1、创建用户对象
SysUser sysUser = new SysUser();
// 2、设置模型值(cookie,header)
HttpContext httpContext = bindingContext.HttpContext;
IRequestCookieCollection cookies = httpContext.Request.Cookies;
string UserId = cookies["UserId"];
string UserName = cookies["UserName"];
// 1、判断UserId是否为空
if (string.IsNullOrEmpty(UserId))
{
throw new BizException("授权失败,没有登录");
}
sysUser.UserId = Convert.ToInt32(UserId);
sysUser.UserName = UserName;
// 3、返回结果
bindingContext.Result = ModelBindingResult.Success(sysUser);
}
return Task.CompletedTask;
}
}
- 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
/// <summary>
/// 系统用户模型绑定(从头部信息获取用户信息,UserId和UserName)
/// 1、将HttpContext用户信息转换成为Sysuser
/// 2、将Sysuser绑定到action方法参数
/// </summary>
public class SysUserHeaderModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
if (bindingContext.ModelType == typeof(SysUser))
{
// 1、创建用户对象
SysUser sysUser = new SysUser();
// 2、设置模型值(cookie,header)
HttpContext httpContext = bindingContext.HttpContext;
IHeaderDictionary headers = httpContext.Request.Headers;
string UserId = headers["UserId"];
string UserName = headers["UserName"];
// 1、判断UserId是否为空
if (string.IsNullOrEmpty(UserId))
{
throw new BizException("授权失败,没有登录");
}
sysUser.UserId = Convert.ToInt32(UserId);
sysUser.UserName = UserName;
// 3、返回结果
bindingContext.Result = ModelBindingResult.Success(sysUser);
}
return Task.CompletedTask;
}
}
- 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
/// <summary>
/// 系统用户模型绑定
/// 1、将HttpContext用户信息转换成为Sysuser
/// 2、将Sysuser绑定到action方法参数
/// </summary>
public class SysUserModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
if (bindingContext.ModelType == typeof(SysUser))
{
// 1、转换到指定模型
// SysUser sysUser = (SysUser)bindingContext.Model;
SysUser sysUser = new SysUser();
// 2、设置模型值(cookie,header)
HttpContext httpContext = bindingContext.HttpContext;
ClaimsPrincipal claimsPrincipal = httpContext.User;
IEnumerable<Claim> claims = claimsPrincipal.Claims;
// 1、判断申明是否为空,如果为空,没有登录,抛出异常
if (claims.ToList().Count == 0)
{
throw new BizException("授权失败,没有登录");
}
foreach (var claim in claims)
{
// 1、获取用户Id
if (claim.Type.Equals("sub"))
{
sysUser.UserId = Convert.ToInt32(claim.Value);
}
// 2、获取用户名
if (claim.Type.Equals("amr"))
{
sysUser.UserName = claim.Value;
}
}
// 3、返回结果
bindingContext.Result = ModelBindingResult.Success(sysUser);
}
return Task.CompletedTask;
}
}
- 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
/// <summary>
/// 自定义模型绑定提供者(提供SysUserModelBinder)
/// </summary>
public class SysUserModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.Metadata.ModelType == typeof(SysUser))
{
return new BinderTypeModelBinder(typeof(SysUserHeaderModelBinder));
}
return null;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/117408988
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)