public IActionResult Contact() { StmPrincipal principal = new StmPrincipal(); ClaimsIdentity identity = new ClaimsIdentity(); identity.AddClaim(new Claim(Core.Security.ClaimTypes.Permissions, "GetId")); identity.AddClaim(new Claim(Core.Security.ClaimTypes.Id, "1")); identity.AddClaim(new Claim(Core.Security.ClaimTypes.Username, "ADMIN")); principal.AddIdentity(identity); var stmPrincipalPersistor = HttpContext.RequestServices.GetService <IStmPrincipalPersistor>(); stmPrincipalPersistor.SavePrincipal(principal); ViewData["Message"] = "Your contact page."; return(View()); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime, ILoggerFactory loggerFactory, IOptions <ServiceInfoRegisterConfig> consulCfg) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //服务注册 app.RegisterConsul(lifetime, consulCfg); //添加日志 loggerFactory.AddLog4Net(); //配置微服务服务端 app.UseStmHttpMicroServiceServer( new ServiceHandleMap() .AddHandle <ISysConfigService>() .AddHandle <INumberIdService>() .AddHandle <IResService>() .AddHandle <IAuthService>(), new HttpExceptionPolicy() //把其他异常转换成BaseException .TransException <FormatException>(101) //只接受列表里的错误,并把错误码发给客户端 .HandleBaseException( new int[] { 1, //一般性错误,可直接提示用户 12, //权限不足 13, 14, 15, 14, 17, 18, 19, 20, 21 }, ctx => { var exp = ctx.Exception.GetExceptionOfType <BaseException>(); var rsp = new { stm_remote_statuscode = exp.Code, stm_remote_message = exp.Message }; ctx.Environment.Response.StatusCode = 500; ctx.Environment.Response.ContentType = "application/json; charset=utf-8"; ctx.Environment.Response.Headers.Add("stm_remote_statuscode", exp.Code.ToString()); ctx.Environment.Response.WriteAsync(JsonConvert.SerializeObject(rsp)).Wait(); ctx.ExceptionIsHandled = true; }, true) //其余异常全部返回99错误码 .Handle(ctx => true, ctx => { var exp = ctx.Exception; var rsp = new { stm_remote_statuscode = StandradErrorCodes.UnkonwError, stm_remote_message = exp.Message, stm_remote_stacktrace = exp.StackTrace }; ctx.Environment.Response.StatusCode = 500; ctx.Environment.Response.ContentType = "application/json; charset=utf-8"; ctx.Environment.Response.Headers.Add("stm_remote_statuscode", StandradErrorCodes.UnkonwError.ToString()); ctx.Environment.Response.WriteAsync(JsonConvert.SerializeObject(rsp)).Wait(); ctx.ExceptionIsHandled = true; }, true) ); app.Map("/setp", ap => ap.Run(async context => { StmPrincipal principal = new StmPrincipal(); ClaimsIdentity identity = new ClaimsIdentity(); identity.AddClaim(new Claim(Core.Security.ClaimTypes.Permissions, "GetId")); principal.AddIdentity(identity); var stmPrincipalPersistor = context.RequestServices.GetService <IStmPrincipalPersistor>(); stmPrincipalPersistor.SavePrincipal(principal); await context.Response.WriteAsync("已授权getid"); })); //其他流量 app.Run(async httpcontext => { var db = httpcontext.RequestServices.GetService <CommonDb>(); //var manager = ((Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies)db).StateManager; await httpcontext.Response.WriteAsync("ST API SERVICE"); }); }