public IActionResult GetUrlToLocal(string url, string static_dirname, string indexname) { var remsg = string.Empty; if (string.IsNullOrEmpty(url)) { return(new JsonResult(new { isok = false, msg = remsg })); } try { var urlTest = new Uri(url); HttpClient client = new System.Net.Http.HttpClient(); var response = client.GetAsync(urlTest).Result; var allresult = response.Content.ReadAsStringAsync().Result; var savedPageName = ""; if (string.IsNullOrEmpty(indexname)) { var indexfileName = url.Substring(url.LastIndexOf('/') + 1); if (string.IsNullOrEmpty(indexfileName)) { savedPageName = Guid.NewGuid().ToString("N").Replace("-", ""); } else { savedPageName = indexfileName; } } else { savedPageName = indexname; } //System.IO.File.WriteAllText(Path.Combine(APPCommon.UserUploadBaseDir, savedPageName), allresult); Encoding encoder = Encoding.GetEncoding("utf-8"); HtmlWeb webClient = new HtmlWeb(); HtmlDocument htmlDoc = webClient.Load(url); HtmlNodeCollection hrefList = htmlDoc.DocumentNode.SelectNodes(".//a[@href]"); HtmlNodeCollection scriptList = htmlDoc.DocumentNode.SelectNodes(".//script[@src]"); HtmlNodeCollection cssList = htmlDoc.DocumentNode.SelectNodes(".//link[@href]"); HtmlNodeCollection imgList = htmlDoc.DocumentNode.SelectNodes(".//img[@src]"); foreach (var hr in scriptList) { var href = hr.GetAttributeValue("src", ""); DownAndSaveFile(static_dirname, urlTest, href); ReplaceUrl("src", static_dirname, urlTest, hr, href); } foreach (var hr in cssList) { var href = hr.GetAttributeValue("href", ""); DownAndSaveFile(static_dirname, urlTest, href); ReplaceUrl("href", static_dirname, urlTest, hr, href); } foreach (var hr in imgList) { var href = hr.GetAttributeValue("src", ""); DownAndSaveImgFile(static_dirname, urlTest, href); ReplaceUrl("src", static_dirname, urlTest, hr, href); } htmlDoc.Save(Path.Combine(APPCommon.UserUploadBaseDir, savedPageName)); //System.IO.File.WriteAllText(Path.Combine(APPCommon.UserUploadBaseDir, savedPageName), allresult); } catch (Exception exception) { Logger.LogDebug(LoggerName, LoggerHelper.GetExceptionString(exception)); } return(new JsonResult(new { isok = true, msg = remsg })); //return View(); }
//public void GetRequestHash(ActionExecutingContext actionExecutingContext) //{ // //actionExecutingContext.HttpContext.req //} private async static Task RunComposity(int requsetHash, HttpContext httpContext, RuntimeStepModel newrunmodel, ISqlSugarClient dbFactory, ISysDatabaseService codeService, IConfiguration config) { var cons = newrunmodel.ComposeEntity; if (cons.RunMode == RunMode.Coding) { if (newrunmodel.ComposeTemplate != null) { //TODO :add template } //Manager.RuntimeModels.Add(newrunmodel); else { var steps = FindSteps(cons.Guid, codeService); foreach (var s in steps) { object rebject = null; object DynaObject = null; if (s.IsUsingAuth.ToBool()) { await httpContext.AuthenticateAsync(); if (httpContext.User.Identity.Name != s.UserName || !httpContext.User.Identity.IsAuthenticated) { APPCommon.ResponseUnAuth((ActionExecutingContext)newrunmodel.Res["__actioncontext"], s.LoginUrl); return; } } var cacheKey = string.Concat(cons.Guid, "_", s.ExcuteType, "_", s.FlowStepType, "_", s.Guid, "_", s.ArgNames); object stepResult = APP.RuntimeCache.GetOrCreate(cacheKey, entry => { object newobj = null; APP.RuntimeCache.Set(cacheKey, newobj); return(newobj); }); if (s.IsUsingCache && stepResult != null) { rebject = stepResult; } else { if (!s.IsUsingCache || stepResult == null) { switch (s.FlowStepType) { case FlowStepType.Declare: { lock (lockobj) { //root.Usings[2].Name.ToString() // var rebject2 = Manager.GetData(spec.InParamter1, spec.InParamter2); var runcode = APP.FindOrAddRumtimeCode(s.Guid); var so_default = ScriptOptions.Default; if (runcode == null) { runcode = new RuntimeCode() { StepGuid = s.Guid, CodeLines = s.ProxyCode, }; List <string> dlls = new List <string>(); var isref = false; string dllbase = APPCommon.AppRootDir; if (!string.IsNullOrEmpty(s.TypeLib)) { var dllfile = dllbase + s.TypeLib; if (APP.DynamicReferenceDlls.Contains(dllfile)) { isref = false; } else { APP.DynamicReferenceDlls.Add(dllfile); isref = true; dlls.Add(dllfile); } } if (!string.IsNullOrEmpty(s.ReferenceDlls)) { isref = true; string[] dllnams = s.ReferenceDlls.Split(','); foreach (var n in dllnams) { APP.DynamicReferenceDlls.Add(dllbase + n); } dlls.AddRange(dllnams); } if (isref) { so_default = so_default.WithReferences(dlls.ToArray()); } so_default = so_default.WithReferences(Assembly.GetExecutingAssembly()); var state = CSharpScript.Create <object>(s.ProxyCode, so_default, typeof(Dictionary <string, object>)); runcode.Script = state; APP.RuntimeCodes.Add(s.Guid, runcode); } if (!string.IsNullOrEmpty(s.ReferenceDlls)) { string dllbase = APPCommon.AppRootDir; } rebject = runcode.Script.RunAsync(newrunmodel.Res).Result.ReturnValue; Logger.LogInfo(LoggerName, $"AComposityId id {s.AComposityId} ,step {s.StepOrder}, Declare: result {rebject} "); } } break; case FlowStepType.GetData: { DynaObject = HandleGetData(httpContext, newrunmodel, dbFactory, s); } break; case FlowStepType.CallMethod: { var methodsub = APP.GetMethodFromConfig(s.IsBuildIn.Value, s.TypeLib, s.TypeFullName, s.MethodName); var objParams = new List <object>(); if (!string.IsNullOrEmpty(s.ArgNames)) { objParams = newrunmodel.GetPoolResuce(s.ArgNames.Split(',')); } else { objParams = null; } try { if (methodsub.IsStatic) { DynaObject = methodsub.Invoke(null, objParams.ToArray()); } else { var instance = newrunmodel.Res[s.InstanceName]; DynaObject = methodsub.Invoke(instance, objParams.ToArray()); } } catch (Exception exp) { Logger.LogError(logName, "Run -> FlowStepType.CallMethod error,composity:{0},step:{1},-------------exception:{2}", cons.Id, s.Guid, LoggerHelper.GetExceptionString(exp)); break; } Logger.LogInfo(LoggerName, $"AComposityId id {s.AComposityId} ,step {s.StepOrder}, CallMethod {s.MethodName}: result {DynaObject} "); } break; case FlowStepType.Text: { rebject = s.OutPutText; } break; case FlowStepType.Function: { DynaObject = DealTheFunction(newrunmodel, s, httpContext); Logger.LogInfo(LoggerName, $"DealTheFunction: result {DynaObject} "); } break; case FlowStepType.RazorKey: try { rebject = stepResult = await HandleRazorKey(newrunmodel, s); } catch (Exception exp) { Logger.LogError(logName, $"parser RazorText wrong: {exp.Message}-------{LoggerHelper.GetExceptionString(exp)}"); } break; case FlowStepType.RazorText: try { rebject = stepResult = await HandleRazorText(newrunmodel, s, httpContext, s.ProxyCode); } catch (Exception exp) { Logger.LogError(logName, $"parser RazorText wrong: {exp.Message}-------{LoggerHelper.GetExceptionString(exp)}"); } break; case FlowStepType.RazorFile: try { var filePath = s.ProxyCode; string template = File.ReadAllText(Path.Combine(APPCommon.UserUploadBaseDir, filePath)); rebject = stepResult = await HandleRazorText(newrunmodel, s, httpContext, template); } catch (Exception exp) { Logger.LogError(logName, "parser RazorFile wrong: " + exp.Message + "-------" + LoggerHelper.GetExceptionString(exp)); } break; } if (rebject == null) { rebject = MagicExtension.BackToInst(DynaObject); } if (s.IsUsingCache) { APP.RuntimeCache.Set(cacheKey, rebject); } } } if (!string.IsNullOrEmpty(s.StorePoolKey) && rebject != null) { newrunmodel.SetComposityResourceValue(s.StorePoolKey, rebject); } } await CheckAndRunNextRuntimeComposity(requsetHash, httpContext, newrunmodel, dbFactory, codeService, config); } //Manager.RuntimeModels.Remove(newrunmodel); } }
private static void SetupIdentity(IServiceCollection services, AppConfig appConfig, string signingkey, string backendfoldername, string AuthorizeCookiesName, string OdicCookiesName, string identityserver4url, string Identityserver4ApiResouceKey, string idsr4_ClientId, string idsr4_ClientSecret, string OauthClientConfig_scopes, bool IsUsingIdentityserverClient, bool IsUsingIdentityserver4, bool IsUsingLocalIndentity, string idsr4_ReponseType) { var defautUserLoginUrl = appConfig.GetDicstring("DefaultUser.LoginUrl"); var defautUserLogoutUrl = appConfig.GetDicstring("DefaultUser.LogoutUrl"); var defautUserRegUrl = appConfig.GetDicstring("DefaultUser.RegUrl"); var defautUserForbiddenl = appConfig.GetDicstring("DefaultUser.Forbidden"); var permissionRequirement = new UserPermissionRequirement( "user", deniedAction: "/home/denied", loginPath: "/home/login", claimType: ClaimTypes.Role, TimeSpan.FromHours(12)); // 授权 services.AddAuthorization(options => { options.AddPolicy("policy", policy => { //policy.RequireRole("admin"); policy.Requirements.Add(permissionRequirement); }); }); if (IsUsingLocalIndentity) { var s = services.AddAuthentication( options => { if (IsUsingIdentityserverClient == false || IsUsingIdentityserver4 == false) { options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; } } ) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => { options.LoginPath = new PathString(defautUserLoginUrl); options.AccessDeniedPath = new PathString(defautUserForbiddenl); options.LogoutPath = defautUserLogoutUrl; //指定登出的路径 options.ExpireTimeSpan = TimeSpan.FromDays(30); //指定Cookie的过期时间 options.SlidingExpiration = true; //当Cookie过期时间已达一半时,是否重置为ExpireTimeSpan options.Events = new CookieAuthenticationEvents //可用于拦截和重写Cookie身份验证 { //OnValidatePrincipal = Filter.LastChangedValidator.ValidateAsync }; options.Events.OnRedirectToLogin = z => //api接口判断 { if (z.HttpContext.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase)) { z.HttpContext.Response.Redirect("/api/Login/UnAuth"); //未授权错误信息的接口地址,返回json } else { z.HttpContext.Response.Redirect(z.RedirectUri); //其它安装默认处理 } return(Task.CompletedTask); }; //options.Cookie.Name = "AuthCookie"; ////options.Cookie.Domain = "contoso.com"; //options.Cookie.Path = "/"; options.Cookie.HttpOnly = true; options.Cookie.SameSite = SameSiteMode.Lax; }) .AddCookie(AdminAuthorizeAttribute.CustomerAuthenticationScheme, options => { //options.LoginPath = new PathString(defautUserLoginUrl); //options.AccessDeniedPath = new PathString(defautUserForbiddenl); //options.LogoutPath = defautUserLogoutUrl;//指定登出的路径 options.LoginPath = new PathString("/" + backendfoldername + "/Admin/Signin"); options.AccessDeniedPath = new PathString("/Error/Forbidden"); options.LogoutPath = "/" + backendfoldername + "/Admin/Index"; //指定登出的路径 options.ExpireTimeSpan = TimeSpan.FromDays(30); //指定Cookie的过期时间 options.SlidingExpiration = true; //当Cookie过期时间已达一半时,是否重置为ExpireTimeSpan options.Events = new CookieAuthenticationEvents //可用于拦截和重写Cookie身份验证 { //OnValidatePrincipal = Filter.LastChangedValidator.ValidateAsync }; options.Events.OnRedirectToLogin = z =>//api接口判断 { if (z.HttpContext.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase)) { z.HttpContext.Response.Redirect("/api/Login/UnAuth");//未授权错误信息的接口地址,返回json } else { z.HttpContext.Response.Redirect(z.RedirectUri);//其它安装默认处理 } return(Task.CompletedTask); }; options.Cookie.HttpOnly = true; options.Cookie.SameSite = SameSiteMode.Lax; }); s.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => { if (IsUsingIdentityserver4) { //options.JwtValidationClockSkew = TimeSpan.FromSeconds(0); options.Authority = identityserver4url; // IdentityServer的地址 options.RequireHttpsMetadata = false; // 不需要Https options.Audience = Identityserver4ApiResouceKey; // 和资源名称相对应 } else { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(signingkey)),//秘钥 ValidateIssuer = true, ValidIssuer = appConfig.GetDicstring("JWT.Issuer"), ValidateAudience = true, ValidAudience = appConfig.GetDicstring("JWT.Audience"), ValidateLifetime = true, ClockSkew = TimeSpan.FromMinutes(5) }; } options.TokenValidationParameters.ClockSkew = TimeSpan.FromMinutes(1); // 我们要求 Token 需要有超时时间这个参数 options.TokenValidationParameters.RequireExpirationTime = true; //}; }); } if (IsUsingIdentityserver4 && !IsUsingLocalIndentity) { services.AddMvcCore(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => { options.Authority = identityserver4url; // IdentityServer的地址 options.RequireHttpsMetadata = false; // 不需要Https options.Audience = Identityserver4ApiResouceKey; // 和资源名称相对应 options.TokenValidationParameters.ClockSkew = TimeSpan.FromMinutes(1); options.TokenValidationParameters.RequireExpirationTime = true; }); } if (IsUsingIdentityserverClient) { services.AddAuthentication(options => { // 使用cookie来本地登录用户(通过DefaultScheme = "Cookies") options.DefaultScheme = AuthorizeCookiesName; // 设置 DefaultChallengeScheme = "oidc" 时,表示我们使用 OIDC 协议 options.DefaultChallengeScheme = OdicCookiesName; }) // 我们使用添加可处理cookie的处理程序 .AddCookie(AuthorizeCookiesName) // 配置执行OpenID Connect协议的处理程序 .AddOpenIdConnect(OdicCookiesName, options => { // options.SignInScheme = AuthorizeCookiesName; // 表明我们信任IdentityServer客户端 options.Authority = identityserver4url; // 表示我们不需要 Https options.RequireHttpsMetadata = false; // 用于在cookie中保留来自IdentityServer的 token,因为以后可能会用 options.SaveTokens = true; try { options.ClientId = idsr4_ClientId; // "mvc_client"; options.ClientSecret = idsr4_ClientSecret; options.CallbackPath = "/public/callback.html"; //options.TokenEndpoint = "/Admin/Signin"; options.ResponseType = idsr4_ReponseType; } catch (Exception exp) { Logger.LogError(LoggerName, "exp: " + exp.Message + ",------------->" + LoggerHelper.GetExceptionString(exp)); } options.Scope.Clear(); var allscopes = OauthClientConfig_scopes.Split(','); foreach (var ss in allscopes) { options.Scope.Add(ss); } }) ; } }