Example #1
0
        public async System.Threading.Tasks.Task WriteAuditLogAsync(StmPrincipal stmPrincipal, string content, AuditAdditional auditAdditional)
        {
            AuditLogInfo auditLogInfo = new AuditLogInfo();

            auditLogInfo.AuditLogId = Guid.NewGuid().ToString("N");
            auditLogInfo.Content    = (content ?? "").Length < 255 ? content : content.Substring(0, 255);
            auditLogInfo.LogDt      = DateTime.Now;
            auditLogInfo.UserId     = stmPrincipal?.Claims.FirstOrDefault(t => t.Type == ClaimTypes.Id)?.Value;
            auditLogInfo.UserName   = stmPrincipal?.Claims.FirstOrDefault(t => t.Type == ClaimTypes.Username)?.Value;
            auditLogInfo.Ip         = auditAdditional?.Ip;

            _dbContext.Add(auditLogInfo);

            await _dbContext.SaveChangesAsync();
        }
Example #2
0
        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());
        }
        public void SavePrincipal(StmPrincipal principal)
        {
            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_secretKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var jwttoken = new JwtSecurityToken(
                null,
                null,
                principal.Claims,
                DateTime.Now,
                DateTime.Now.AddMinutes(_expireMinutes),
                creds
                );

            var token = new JwtSecurityTokenHandler().WriteToken(jwttoken);

            _httpContextAccessor.HttpContext.Response.Cookies.Append(_keyname, token, new CookieOptions {
                HttpOnly = true
                           //,IsEssential = true
            });
        }
Example #4
0
        // 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");
            });
        }