文章目录
前言一、Autofac的使用
1.装置包2.效劳注入3.AddModule扩展方法详解
3.1 AppSetting.Init
3.2 Autofac相关效劳类
3.2.1 UserContext3.2.2 ActionObserver3.2.3 ObjectModelValidatorState
前言
IoC容器是一种设计形式,它可以管理应用程序中的对象依赖关系。它可以自动创建和注入对象,从而减少了代码的耦合度和反复性。
Autofac是Microsoft .NET的IoC容器。它管理类之间的依赖关系,以便应用程序在大小和复杂性增加时易于更改。这是通过将常规 .NET 类视为组件来实现的。
Autofac的官网:https://autofac.org/
Autofac的源码网址:https://gitee.com/dennisdyh/Autofac.Extensions.DependencyInjection
一、Autofac的使用
1.装置包
- Autofac.Extensions.DependencyInjection
复制代码
2.效劳注入
在Program注入Autofac效劳- publicstaticIHostBuilderCreateHostBuilder(string[] args)=>
- Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{
- webBuilder.ConfigureKestrel(serverOptions =>{
- serverOptions.Limits.MaxRequestBodySize =10485760;// Set properties and call methods on options});
- webBuilder.UseKestrel().UseUrls("http://*:9991");
- webBuilder.UseIIS();
- webBuilder.UseStartup<Startup>();}).UseServiceProviderFactory(newAutofacServiceProviderFactory());//设置工厂来交换实例
复制代码
在Startup中增加ConfigureContainer方法,用来配置映射关系- publicvoidConfigureContainer(ContainerBuilder builder){
- Services.AddModule(builder, Configuration);}
复制代码 使用了Autofac以后,在IServiceCollection中注入的效劳,也能生效;因为Autofac是先接受了所有的来自于IServiceCollection的效劳映射后,再去读取ConfigureContainer方法中配置的映射。
3.AddModule扩展方法详解
AddModule包含了反射注入容器和一堆效劳注入容器- // private static bool _isMysql = false;publicstaticIServiceCollectionAddModule(thisIServiceCollection services,ContainerBuilder builder,IConfiguration configuration){//services.AddSession();//services.AddMemoryCache();//初始化配置文件
- AppSetting.Init(services, configuration);Type baseType =typeof(IDependency);var compilationLibrary = DependencyContext.Default
- .RuntimeLibraries
- .Where(x =>!x.Serviceable
- && x.Type =="project").ToList();var count1 = compilationLibrary.Count;List<Assembly> assemblyList =newList<Assembly>();foreach(var _compilation in compilationLibrary){try{
- assemblyList.Add(AssemblyLoadContext.Default.LoadFromAssemblyName(newAssemblyName(_compilation.Name)));}catch(Exception ex){
- Console.WriteLine(_compilation.Name + ex.Message);}}
- builder.RegisterAssemblyTypes(assemblyList.ToArray()).Where(type => baseType.IsAssignableFrom(type)&&!type.IsAbstract).AsSelf().AsImplementedInterfaces().InstancePerLifetimeScope();
- builder.RegisterType<UserContext>().InstancePerLifetimeScope();
- builder.RegisterType<ActionObserver>().InstancePerLifetimeScope();//model校验结果
- builder.RegisterType<ObjectModelValidatorState>().InstancePerLifetimeScope();string connectionString = DBServerProvider.GetConnectionString(null);if(DBType.Name == DbCurrentType.MySql.ToString()){//增加dapper对mysql字段Guid映射
- SqlMapper.AddTypeHandler(newDapperParseGuidTypeHandler());
- SqlMapper.RemoveTypeMap(typeof(Guid?));//services.AddDbContext<SysDbContext>();//mysql8.x的版本使用Pomelo.EntityFrameworkCore.MySql 3.1会产生异常,需要在字符串连接上添加allowPublicKeyRetrieval=true
- services.AddDbContextPool<SysDbContext>(optionsBuilder =>{ optionsBuilder.UseMySql(connectionString);},64);
- services.AddDbContextPool<ServiceDbContext>(optionsBuilder =>{ optionsBuilder.UseMySql(connectionString);},64);
- services.AddDbContextPool<ReportDbContext>(optionsBuilder =>{ optionsBuilder.UseMySql(connectionString);},64);}elseif(DBType.Name == DbCurrentType.PgSql.ToString()){
- services.AddDbContextPool<SysDbContext>(optionsBuilder =>{ optionsBuilder.UseNpgsql(connectionString);},64);
- services.AddDbContextPool<ServiceDbContext>(optionsBuilder =>{ optionsBuilder.UseNpgsql(connectionString);},64);
- services.AddDbContextPool<ReportDbContext>(optionsBuilder =>{ optionsBuilder.UseNpgsql(connectionString);},64);}else{
- services.AddDbContextPool<SysDbContext>(optionsBuilder =>{ optionsBuilder.UseSqlServer(connectionString);},64);
- services.AddDbContextPool<ServiceDbContext>(optionsBuilder =>{ optionsBuilder.UseSqlServer(connectionString);},64);
- services.AddDbContextPool<ReportDbContext>(optionsBuilder =>{ optionsBuilder.UseSqlServer(connectionString);},64);}//启用缓存if(AppSetting.UseRedis){
- builder.RegisterType<RedisCacheService>().As<ICacheService>().SingleInstance();}else{
- builder.RegisterType<MemoryCacheService>().As<ICacheService>().SingleInstance();}//kafka注入//if (AppSetting.Kafka.UseConsumer)// builder.RegisterType<KafkaConsumer<string, string>>().As<IKafkaConsumer<string, string>>().SingleInstance();//if (AppSetting.Kafka.UseProducer)// builder.RegisterType<KafkaProducer<string, string>>().As<IKafkaProducer<string, string>>().SingleInstance();return services;}
复制代码
3.1 AppSetting.Init
应用程序初始化主要是对配置文件使用- {"Logging":{"LogLevel":{"Default":"Information","Microsoft":"Warning","Microsoft.Hosting.Lifetime":"Information"}},"AllowedHosts":"*","VirtualPath":{"StaticFile":"E:\\Web\\Static",//配置的虚拟目录文件所在途径"FolderName":"/Static"//访问时此途径时的别名},"AppUrls":{},"Connection":{"DBType":"MsSql",//MySql/MsSql/PgSql //数据库类型,假设使用的是sqlserver此处应设置为MsSql//sqlserver连接字符串"DbConnectionString":"Data Source=.;Initial Catalog=iMES_Open;Persist Security Info=True;User ID=sa;Password=1;Connect Timeout=500;",//mysql连接字符串(晋级EFCore3.1到时已将mysql连接字符串修改,2019-12-20)//"DbConnectionString": " Data Source=127.0.0.1;Database=imes_open;AllowLoadLocalInfile=true;User ID=root;Password=123456;allowPublicKeyRetrieval=true;pooling=true;CharSet=utf8;port=3306;sslmode=none;",//PgSql连接字符串// "DbConnectionString": "Host=132.232.2.109;Port=5432;User id=postgres;password=jxx_abcd;Database=netcoredev;","RedisConnectionString":"127.0.0.1,Password=123456,SyncTimeout=15000",//redis连接字符串(最好加密)"UseRedis":"false",//是否使用redis,假设不使用,默认使用Memory内置缓存"UseSignalR":"true"//是否使用SignalR(2022.05.03),注意需要将端的地址配置到下面的CorsUrls属性中},//业务数据库连接"ServiceConnectingString":"Data Source=.;Initial Catalog=iMES;Persist Security Info=True;User ID=sa;Password=123456;Connect Timeout=500;",//报表数据库连接"ReportConnectingString":"Data Source=.;Initial Catalog=iMES;Persist Security Info=True;User ID=sa;Password=123456;Connect Timeout=500;","Secret":{//秘钥配置"JWT":"BB3647441FFA4B5DB4E64A29B53CE525",//JWT"Audience":"iMES.core","Issuer":"iMES.core.owner","User":"C5ABA9E202D94C43A3CA66002BF77FAF",//"DB":"3F8B7B38AD3D484A89ACA513CBD79F36","Redis":"E6D90DDBC70C4F4EA3C312B6FCB473C8"},//================跨域恳求 (CORS)配置(2019-12-20新增),//================.netcore3.1必需配置此属性,多个url用豆号隔开,url为vue站点的地址,可以将发布后的地址也同时配置上"CorsUrls":"http://localhost:8081,http://localhost:8082,http://localhost:8080,http://localhost:7080,http://localhost:9980,http://127.0.0.1:9980,http://localhost:9990,http://www.volcore.xyz,http://app.625sc.com,https://imes.625sc.com","ExpMinutes":"120",//JWT有效期(分钟=默认120),"CreateMember":{//对表插入数据时,需要记录创建人/创建时间/创建日期,配置UserIdField/UserNameField/DateField分别为对应数据库的创建人CreateID,创建人Creator,创建时间CreateDate字段(新建数据时,由框架默认完成给这几个字段赋值,字段区分大小写)或可手动调用T.SetCreateDefaultVal()完成设置创建人/创建时间/创建日期//假设表的主键是GUID,界面查询时默认会用到DateField对应的实体(数据库)字段停止排序"UserIdField":"CreateID","UserNameField":"Creator","DateField":"CreateDate"},"ModifyMember":{//修改同上"UserIdField":"ModifyID","UserNameField":"Modifier","DateField":"ModifyDate"},//演示系统过滤Action,只要超级管理员才干操作,其他用户只要只读权限"GlobalFilter":{"Message":"演示环境,当前帐号没有开启此功能权限","Enable":"false",//开启Action过滤"Actions":["Update","Del","Add","SavePermission","Save","CreatePage","CreateVuePage","CreateEntityModel","SaveEidt","CreateServices","Import","Upload","Audit","ModifyPwd"]},"Kafka":{//是否使用消费者"UseProducer":false,"ProducerSettings":{"BootstrapServers":"192.168.20.241:9092",//confluent cloud bootstrap servers"SaslMechanism":"Plain","SecurityProtocol":"SaslSsl","SaslUsername":"<confluent cloud key>","SaslPassword":"<confluent cloud secret>"},//是否使用消费者"UseConsumer":false,//是否持续监听消费者订阅 用于while循环订阅"IsConsumerSubscribe":true,"ConsumerSettings":{"BootstrapServers":"192.168.20.241:9092",//confluent cloud bootstrap servers"GroupId":"amcl_group",//web-example-group"SaslMechanism":"Plain","SecurityProtocol":"SaslSsl","SaslUsername":"<confluent cloud key>","SaslPassword":"<confluent cloud secret>"},"Topics":{"TestTopic":"alarm_topic"}},"Mail":{"Address":"zm_rid@163.com",//发件的邮箱"Host":"smtp.163.com","Name":"iMES",//发送人名称"Port":25,"EnableSsl":false,"AuthPwd":"受权密码"//受权密码(对应邮箱设置里面去开启)},"QuartzAccessKey":"65EC9387312E4717810C552963CE59FF",//定时任务的值"ExportPDFPath":"C:\\iMES_Open\\iMES_Front\\PDF"//导出PDF途径}
复制代码
- publicstaticvoidInit(IServiceCollection services,IConfiguration configuration){//---------------------------------配置信息效劳类的映射------------------------------
- Configuration = configuration;
- services.Configure<Secret>(configuration.GetSection("Secret"));
- services.Configure<Connection>(configuration.GetSection("Connection"));
- services.Configure<CreateMember>(configuration.GetSection("CreateMember"));
- services.Configure<ModifyMember>(configuration.GetSection("ModifyMember"));
- services.Configure<GlobalFilter>(configuration.GetSection("GlobalFilter"));
- services.Configure<Kafka>(configuration.GetSection("Kafka"));//---------------------------------获取当前程序目录-----------------------------------var provider = services.BuildServiceProvider();IWebHostEnvironment environment = provider.GetRequiredService<IWebHostEnvironment>();
- CurrentPath = Path.Combine(environment.ContentRootPath,"").ReplacePath();//---------------------------------获取各种配置操作类-------------------------------------
- Secret = provider.GetRequiredService<IOptions<Secret>>().Value;//设置修改或删除时需要设置为默认用户信息的字段
- CreateMember = provider.GetRequiredService<IOptions<CreateMember>>().Value ??newCreateMember();
- ModifyMember = provider.GetRequiredService<IOptions<ModifyMember>>().Value ??newModifyMember();//全局过滤行为
- GlobalFilter = provider.GetRequiredService<IOptions<GlobalFilter>>().Value ??newGlobalFilter();
- GlobalFilter.Actions = GlobalFilter.Actions ??newstring[0];//Kafka消息队列
- Kafka = provider.GetRequiredService<IOptions<Kafka>>().Value ??newKafka();//各种数据库和缓存数据库连接字符串
- _connection = provider.GetRequiredService<IOptions<Connection>>().Value;//JWT过期时间
- ExpMinutes =(configuration["ExpMinutes"]??"120").GetInt();//判断数据库是否存在
- DBType.Name = _connection.DBType;if(string.IsNullOrEmpty(_connection.DbConnectionString))thrownewSystem.Exception("未配置好数据库默认连接");//停止数据库解密(解密失败返回原字符串,胜利返回新字符串)try{
- _connection.DbConnectionString = _connection.DbConnectionString.DecryptDES(Secret.DB);}catch{}if(!string.IsNullOrEmpty(_connection.RedisConnectionString)){try{
- _connection.RedisConnectionString = _connection.RedisConnectionString.DecryptDES(Secret.Redis);}catch{}}}
复制代码
3.1.1 数据库字符串加/解密
- publicstaticclassSecurityEncDecryptExtensions{privatestaticbyte[] Keys ={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};/// <summary> /// DES加密字符串 /// </summary> /// <param name="encryptString">待加密的字符串</param> /// <param name="encryptKey">加密密钥,要求为16位</param> /// <returns>加密胜利返回加密后的字符串,失败返回源串</returns> publicstaticstringEncryptDES(thisstring encryptString,string encryptKey){try{byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0,16));byte[] rgbIV = Keys;byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);using(var DCSP = Aes.Create()){using(MemoryStream mStream =newMemoryStream()){using(CryptoStream cStream =newCryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write)){
- cStream.Write(inputByteArray,0, inputByteArray.Length);
- cStream.FlushFinalBlock();return Convert.ToBase64String(mStream.ToArray()).Replace('+','_').Replace('/','~');}}}}catch(Exception ex){thrownewException("密码加密异常"+ ex.Message);}}/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为16位,和加密密钥相同</param> /// <returns>解密胜利返回解密后的字符串,失败返源串</returns> publicstaticstringDecryptDES(thisstring decryptString,string decryptKey){byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0,16));byte[] rgbIV = Keys;byte[] inputByteArray = Convert.FromBase64String(decryptString.Replace('_','+').Replace('~','/'));using(var DCSP = Aes.Create()){using(MemoryStream mStream =newMemoryStream()){using(CryptoStream cStream =newCryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write)){byte[] inputByteArrays =newbyte[inputByteArray.Length];
- cStream.Write(inputByteArray,0, inputByteArray.Length);
- cStream.FlushFinalBlock();return Encoding.UTF8.GetString(mStream.ToArray());}}}}publicstaticboolTryDecryptDES(thisstring decryptString,string decryptKey,outstring result){
- result ="";try{
- result =DecryptDES(decryptString, decryptKey);returntrue;}catch{returnfalse;}}}
复制代码
3.2 Autofac相关效劳类
3.2.1 UserContext
UserContext主要作用就是记录用户信息- publicclassUserContext{/// <summary>/// 为了尽量减少redis或Memory读取,保证执行效率,将UserContext注入到DI,/// 每个UserContext的属性至多读取一次redis或Memory缓存从而进步查询效率/// </summary>publicstaticUserContext Current
- {get{return Context.RequestServices.GetService(typeof(UserContext))asUserContext;}}privatestaticMicrosoft.AspNetCore.Http.HttpContext Context
- {get{return Utilities.HttpContext.Current;}}privatestaticICacheService CacheService
- {get{returnGetService<ICacheService>();}}privatestaticTGetService<T>()whereT:class{return AutofacContainerModule.GetService<T>();}publicUserInfo UserInfo
- {get{if(_userInfo !=null){return _userInfo;}returnGetUserInfo(UserId);}}privateUserInfo _userInfo {get;set;}/// <summary>/// 角色ID为1的默认为超级管理员/// </summary>publicbool IsSuperAdmin
- {get{returnIsRoleIdSuperAdmin(this.RoleId);}}/// <summary>/// 角色ID为1的默认为超级管理员/// </summary>publicstaticboolIsRoleIdSuperAdmin(int roleId){return roleId ==1;}publicUserInfoGetUserInfo(int userId){if(_userInfo !=null)return _userInfo;if(userId <=0){
- _userInfo =newUserInfo();return _userInfo;}string key = userId.GetUserIdKey();
- _userInfo = CacheService.Get<UserInfo>(key);if(_userInfo !=null&& _userInfo.User_Id >0)return _userInfo;
- _userInfo = DBServerProvider.DbContext.Set<Sys_User>().Where(x => x.User_Id == userId).Select(s =>newUserInfo(){
- User_Id = userId,
- Role_Id = s.Role_Id.GetInt(),
- RoleName = s.RoleName,//2022.08.15增加部门id
- DeptId = s.Dept_Id??0,
- Token = s.Token,
- UserName = s.UserName,
- UserTrueName = s.UserTrueName,
- Enable = s.Enable
- }).FirstOrDefault();if(_userInfo !=null&& _userInfo.User_Id >0){
- CacheService.AddObject(key, _userInfo);}return _userInfo ??newUserInfo();}/// <summary>/// 获取角色权限时通过安全字典锁定的角色id/// </summary>privatestaticConcurrentDictionary<string,object> objKeyValue =newConcurrentDictionary<string,object>();/// <summary>/// 角色权限的版本号/// </summary>privatestaticreadonlyDictionary<int,string> rolePermissionsVersion =newDictionary<int,string>();/// <summary>/// 每个角色ID对应的菜单权限(已做静态化处置)/// 每次获取权限时用当前效劳器的版本号与redis/memory缓存的版本比较,假设不同会重新刷新缓存/// </summary>privatestaticreadonlyDictionary<int, List<Permissions>> rolePermissions =newDictionary<int, List<Permissions>>();/// <summary>/// 获取用户所有的菜单权限/// </summary>publicList<Permissions> Permissions
- {get{returnGetPermissions(RoleId);}}/// <summary>/// 菜单按钮变卦时,同时刷新权限缓存2022.05.23/// </summary>/// <param name="menuId"></param>publicvoidRefreshWithMenuActionChange(int menuId){foreach(var roleId in rolePermissions.Where(c => c.Value.Any(x => x.Menu_Id == menuId)).Select(s => s.Key)){if(rolePermissionsVersion.ContainsKey(roleId)){
- CacheService.Add(roleId.GetRoleIdKey(), DateTime.Now.ToString("yyyyMMddHHMMssfff"));}}}/// <summary>/// 获取单个表的权限/// </summary>/// <param name="tableName"></param>/// <returns></returns>publicPermissionsGetPermissions(string tableName){returnGetPermissions(RoleId).Where(x => x.TableName == tableName).FirstOrDefault();}/// <summary>/// 2022.03.26/// 菜单类型1:挪动端,0:PC端/// </summary>publicstaticint MenuType
- {get{return Context.Request.Headers.ContainsKey("uapp")?1:0;}}/// <summary>/// 自定条件查询权限/// </summary>/// <param name="func"></param>/// <returns></returns>publicPermissionsGetPermissions(Func<Permissions,bool> func){// 2022.03.26增挪动端加菜单类型判断returnGetPermissions(RoleId).Where(func).Where(x => x.MenuType == MenuType).FirstOrDefault();}privateList<Permissions>ActionToArray(List<Permissions> permissions){
- permissions.ForEach(x =>{try{var menuAuthArr = x.MenuAuth.DeserializeObject<List<Sys_Actions>>();
- x.UserAuthArr =string.IsNullOrEmpty(x.UserAuth)?newstring[0]: x.UserAuth.Split(",").Where(c => menuAuthArr.Any(m => m.Value == c)).ToArray();}catch{}finally{if(x.UserAuthArr ==null){
- x.UserAuthArr =newstring[0];}}});return permissions;}privateList<Permissions>MenuActionToArray(List<Permissions> permissions){
- permissions.ForEach(x =>{try{
- x.UserAuthArr =string.IsNullOrEmpty(x.UserAuth)?newstring[0]: x.UserAuth.DeserializeObject<List<Sys_Actions>>().Select(s => s.Value).ToArray();}catch{}finally{if(x.UserAuthArr ==null){
- x.UserAuthArr =newstring[0];}}});return permissions;}publicList<Permissions>GetPermissions(int roleId){if(IsRoleIdSuperAdmin(roleId)){//2020.12.27增加菜单界面上不显示,但可以分配权限var permissions = DBServerProvider.DbContext.Set<Sys_Menu>().Where(x => x.Enable ==1|| x.Enable ==2).Select(a =>newPermissions{
- Menu_Id = a.Menu_Id,
- ParentId = a.ParentId,//2020.05.06增加默认将表名转换成小写,权限验证时不再转换
- TableName =(a.TableName ??"").ToLower(),//MenuAuth = a.Auth,
- UserAuth = a.Auth,// 2022.03.26增挪动端加菜单类型
- MenuType = a.MenuType ??0}).ToList();returnMenuActionToArray(permissions);}ICacheService cacheService = CacheService;string roleKey = roleId.GetRoleIdKey();//角色有缓存,并且当前效劳器的角色版本号与redis/memory缓存角色的版本号相同直接返回静态对象角色权限string currnetVeriosn ="";if(rolePermissionsVersion.TryGetValue(roleId,out currnetVeriosn)&& currnetVeriosn == cacheService.Get(roleKey)){return rolePermissions.ContainsKey(roleId)? rolePermissions[roleId]:newList<Permissions>();}//锁定每个角色,通过安全字典减少锁粒度,否则多个同时角色获取缓存会导致阻塞object objId = objKeyValue.GetOrAdd(roleId.ToString(),newobject());//锁定每个角色lock(objId){if(rolePermissionsVersion.TryGetValue(roleId,out currnetVeriosn)&& currnetVeriosn == cacheService.Get(roleKey)){return rolePermissions.ContainsKey(roleId)? rolePermissions[roleId]:newList<Permissions>();}//没有redis/memory缓存角色的版本号或与当前效劳器的角色版本号不同时,刷新缓存var dbContext = DBServerProvider.DbContext;List<Permissions> _permissions =(from a in dbContext.Set<Sys_Menu>()join b in dbContext.Set<Sys_RoleAuth>()on a.Menu_Id equals b.Menu_Id
- whereb.Role_Id == roleId //&& a.ParentId > 0&& b.AuthValue !=""orderby a.ParentId
- selectnewPermissions{
- Menu_Id = a.Menu_Id,
- ParentId = a.ParentId,//2020.05.06增加默认将表名转换成小写,权限验证时不再转换
- TableName =(a.TableName ??"").ToLower(),
- MenuAuth = a.Auth,
- UserAuth = b.AuthValue ??"",// 2022.03.26增挪动端加菜单类型
- MenuType = a.MenuType ??0}).ToList();ActionToArray(_permissions);string _version = cacheService.Get(roleKey);//生成一个唯一版本号标识if(_version ==null){
- _version = DateTime.Now.ToString("yyyyMMddHHMMssfff");//将版本号写入缓存
- cacheService.Add(roleKey, _version);}//刷新当前效劳器角色的权限
- rolePermissions[roleId]= _permissions;//写入当前效劳器的角色最新版本号
- rolePermissionsVersion[roleId]= _version;return _permissions;}}/// <summary>/// 判断是否有权限/// </summary>/// <param name="tableName"></param>/// <param name="authName"></param>/// <param name="roleId"></param>/// <returns></returns>publicboolExistsPermissions(string tableName,string authName,int roleId =0){if(roleId <=0) roleId = RoleId;
- tableName = tableName.ToLower();returnGetPermissions(roleId).Any(x => x.TableName == tableName && x.UserAuthArr.Contains(authName));}/// <summary>/// 判断是否有权限/// </summary>/// <param name="tableName"></param>/// <param name="authName"></param>/// <param name="roleId"></param>/// <returns></returns>publicboolExistsPermissions(string tableName,ActionPermissionOptions actionPermission,int roleId =0){returnExistsPermissions(tableName, actionPermission.ToString(), roleId);}publicint UserId
- {get{return(Context.User.FindFirstValue(JwtRegisteredClaimNames.Jti)?? Context.User.FindFirstValue(ClaimTypes.NameIdentifier)).GetInt();}}publicstring UserName
- {get{return UserInfo.UserName;}}publicstring UserTrueName
- {get{return UserInfo.UserTrueName;}}publicstring Token
- {get{return UserInfo.Token;}}publicint RoleId
- {get{return UserInfo.Role_Id;}}publicvoidLogOut(int userId){
- CacheService.Remove(userId.GetUserIdKey());}}
复制代码
3.2.2 ActionObserver
ActionObserver记录Action执行的信息- publicclassActionObserver{//public ActionObserver(IHttpContextAccessor httpContextAccessor)//{// this.RequestDate = DateTime.Now;// this.HttpContext = httpContextAccessor.HttpContext;//}/// <summary>/// 记录action执行的开端时间/// </summary>publicDateTime RequestDate {get;set;}/// <summary>/// 当前恳求是否已经写过日志,防止手动与系统自动反复写日志/// </summary>publicbool IsWrite {get;set;}publicHttpContext HttpContext {get;}}
复制代码
3.2.3 ObjectModelValidatorState
ObjectModelValidatorState 对象模型校验效劳- publicclassObjectModelValidatorState{publicObjectModelValidatorState(){this.Status =true;}publicbool Status {get;set;}publicbool HasModelContent {get;set;}publicstring Code {get;set;}publicstring Message {get;set;}}publicclassObjectValidatorResult{publicObjectValidatorResult(){}publicObjectValidatorResult(bool status){this.Status = status;}publicObjectValidatorResultOK(string message){this.Status =true;this.Message = message;returnthis;}publicObjectValidatorResultError(string message){this.Status =false;this.Message = message;returnthis;}publicbool Status {get;set;}publicstring Message {get;set;}}
复制代码
其实在Autofac中还注入了数据库效劳,下一章讲解。 |