Files
Yi.Framework/Yi.Doc.Md/02.框架功能模块教程/05.当前用户.md
2023-12-23 21:14:56 +08:00

42 lines
1.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 简介
如何获取当前请求用户信息?这个问题有很多个答案
常规是通过HttpContext对象进行获取它通常是在ControllerBase中控制器中内置了HttpContext对象
也可以通过依赖注入HttpContext访问器中获取
> 但是他们都不够优雅原因与HttpContext具备了强耦合如果对于没有HttpContext,将会非常的难维护,例如:单元测速
你可以依赖注入使用:`ICurrentUser`
它是瞬态注入,但是它能够获取当前作用域的用户信息
## 如何使用
任何地方,依赖注入:`ICurrentUser`
它包含属性:
``` cs
public interface ICurrentUser
{
//是否授权
public bool IsAuthenticated { get; }
//id
public Guid Id { get; }
//用户名
public string UserName { get; }
//租户id
public Guid TenantId { get; }
//邮件
public string Email { get; }
public bool EmailVerified { get; }
//电话
public string PhoneNumber { get; }
public bool PhoneNumberVerified { get; }
//角色codes
public string[]? Roles { get; }
}
```
直接使用即可
> 注意当前用户功能默认是继承到Core模块所以你无需进行任何引用直接使用即可