public static T JsonToEntity <T>(JavaScriptObject javascriptObj) { return((T)JavaScriptDeserializer. DeserializeFromJson(AjaxPro.JavaScriptSerializer. Serialize(javascriptObj), typeof(T))); }
/// <summary> /// 将 JSON 文本转换为 指定类型的对象 /// </summary> /// <param name="json">JSON 文本</param> /// <param name="type">指定类型</param> /// <returns>对象</returns> public static object FromJson(string json, Type type) { return(JavaScriptDeserializer.DeserializeFromJson(json, type)); }
/// <summary> /// 将 JSON 文本转换为 指定类型的对象 /// </summary> /// <typeparam name="T">指定类型</typeparam> /// <param name="json">JSON 文本</param> /// <returns>对象</returns> public static T FromJson <T>(string json) { return(JavaScriptDeserializer.DeserializeFromJson <T>(json)); }
private static IEnumerable <Milestone> MilestoneParser(string milestones) { return(JavaScriptDeserializer.DeserializeFromJson <List <Milestone> >(milestones)); }
private static IEnumerable <Task> TaskParser(string tasks) { return(JavaScriptDeserializer.DeserializeFromJson <List <Task> >(tasks)); }
public void RunJob() { Percentage = 1; Status = Resource.LdapSettingsStatusCheckingLdapSettings; var tenantID = (int)Id; CoreContext.TenantManager.SetCurrentTenant(tenantID); if (_serializeSettings == null) { _log.ErrorFormat("Can't save default LDAP settings."); Error = Resource.LdapSettingsErrorCantGetLdapSettings; IsCompleted = true; return; } try { var settings = (LDAPSupportSettings)JavaScriptDeserializer.DeserializeFromJson(_serializeSettings, typeof(LDAPSupportSettings)); if (settings == null) { _log.ErrorFormat("Wrong LDAP settings were received from client."); Error = Resource.LdapSettingsErrorCantGetLdapSettings; IsCompleted = true; return; } if (!settings.Server.StartsWith("LDAP://")) { settings.Server = "LDAP://" + settings.Server; } if (!string.IsNullOrEmpty(settings.Password)) { settings.PasswordBytes = InstanceCrypto.Encrypt(new UnicodeEncoding().GetBytes(settings.Password)); } settings.Password = string.Empty; var error = GetError(ADDomain.CheckSettings(settings, _importer)); if (error == string.Empty) { Status = Resource.LdapSettingsStatusSavingSettings; Percentage = 3; if (!SettingsManager.Instance.SaveSettings <LDAPSupportSettings>(settings, tenantID)) { _log.ErrorFormat("Can't save LDAP settings."); Error = Resource.LdapSettingsErrorCantSaveLdapSettings; IsCompleted = true; return; } // for logout old ldap users AddLToSids(); if (settings.EnableLdapAuthentication) { CreateUsersAndGroups(settings); } Percentage = 100; } else { Error = error; } } catch (TenantQuotaException e) { _log.ErrorFormat("TenantQuotaException.", e); Error = Resource.LdapSettingsTenantQuotaSettled; } catch (FormatException) { Error = Resource.LdapSettingsErrorCantCreateUsers; } catch (Exception e) { _log.ErrorFormat("Internal server error.", e); Error = Resource.LdapSettingsInternalServerError; } IsCompleted = true; }
public string SaveSettings(string serializeSettings) { if (Context.User != null && Context.User.Identity != null) { var userInfo = CoreContext.UserManager.GetUsers(((IUserAccount)Context.User.Identity).ID); if (userInfo != Constants.LostUser && userInfo.IsAdmin()) { try { if (string.IsNullOrWhiteSpace(serializeSettings)) { _log.ErrorFormat("SSO settings are null or empty."); return(Resource.SsoSettingsAreEmpty); } Settings = (SsoSettings)JavaScriptDeserializer.DeserializeFromJson(serializeSettings, typeof(SsoSettings)); if (Settings == null) { _log.ErrorFormat("Wrong SSO settings were received from client."); return(Resource.SsoSettingsWrongSerialization); } var messageAction = Settings.EnableSso ? MessageAction.SSOEnabled : MessageAction.SSODisabled; if (Settings.EnableSso) { if (!(string.IsNullOrWhiteSpace(Settings.Issuer) || CheckUri(Settings.Issuer))) { _log.ErrorFormat("Wrong Issuer URL: {0}", Settings.Issuer); return(string.Format(Resource.SsoSettingsWrongURL, Settings.Issuer)); } else { Settings.Issuer = Settings.Issuer.Trim(); } if (!(string.IsNullOrWhiteSpace(Settings.SsoEndPoint) || CheckUri(Settings.SsoEndPoint))) { _log.ErrorFormat("Wrong SsoEndPoint URL: {0}", Settings.SsoEndPoint); return(string.Format(Resource.SsoSettingsWrongURL, Settings.SsoEndPoint)); } else { Settings.SsoEndPoint = Settings.SsoEndPoint.Trim(); } if (!string.IsNullOrWhiteSpace(Settings.SloEndPoint) && !CheckUri(Settings.SloEndPoint)) { _log.ErrorFormat("Wrong SloEndPoint URL: {0}", Settings.SloEndPoint); return(string.Format(Resource.SsoSettingsWrongURL, Settings.SloEndPoint)); } else { Settings.SloEndPoint = Settings.SloEndPoint.Trim(); } if (string.IsNullOrWhiteSpace(Settings.PublicKey)) { _log.ErrorFormat("Wrong PublicKey: {0}", Settings.PublicKey); return(Resource.SsoSettingsWrongPublicKey); } else { Settings.PublicKey = Settings.PublicKey.Trim(); } if (Settings.TokenType != TokenTypes.SAML && Settings.TokenType != TokenTypes.JWT) { _log.ErrorFormat("Wrong token type: {0}", Settings.TokenType); return(Resource.SsoSettingsWrongTokenType); } if ((Settings.ValidationType != ValidationTypes.HMAC_SHA256 && Settings.ValidationType != ValidationTypes.RSA_SHA256 && Settings.ValidationType != ValidationTypes.X509)) { _log.ErrorFormat("Wrong validaion type: {0}", Settings.ValidationType); return(Resource.SsoSettingsWrongValidationType); } if (Settings.TokenType == TokenTypes.SAML && Settings.ValidationType != ValidationTypes.X509) { Settings.ValidationType = ValidationTypes.X509; } } if (!SettingsManager.Instance.SaveSettings(Settings, TenantProvider.CurrentTenantID)) { _log.ErrorFormat("Can't save SSO settings."); return(Resource.SsoSettingsCantSaveSettings); } MessageService.Send(HttpContext.Current.Request, messageAction); } catch (Exception e) { _log.ErrorFormat("Save SSO setting error: {0}.", e); return(Resource.SsoSettingsUnexpectedError); } return(string.Empty); } } _log.ErrorFormat("Insufficient Access Rights by saving sso settings!"); throw new SecurityException(); }
public void ProcessRequest(HttpContext context) { Type t = null; #region prepare the IAjaxProxy/IRestProxy type string className = null; string idPart = null; string[] parts = context.Request.Path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < parts.Length; i++) { if (parts[i].ToLower() == "ext4") { if (i + 1 < parts.Length) { className = parts[i + 1]; if (i + 2 < parts.Length) { idPart = parts[i + 2]; } } break; } } if (className == null) { ResponseError(context, new Exception("Type not set.")); return; } if (className.EndsWith(".ashx")) { className = className.Substring(0, className.Length - 5); } if (idPart != null && idPart.EndsWith(".ashx")) { idPart = idPart.Substring(0, idPart.Length - 5); } if (Utility.Settings != null && Utility.Settings.UrlClassMappings.ContainsKey(className)) { className = Utility.Settings.UrlClassMappings[className] as string; if (context.Trace.IsEnabled) { context.Trace.Write(Constant.AjaxID, "Url match to Type: " + className); } } try { t = Type.GetType(className, true); } catch (Exception ex) { if (context.Trace.IsEnabled) { context.Trace.Write(Constant.AjaxID, "Type not found: " + className); } ResponseError(context, ex); return; } #endregion #region GET via IAjaxProxy if (context.Request.HttpMethod == "GET") { if (!typeof(IAjaxProxy).IsAssignableFrom(t)) { ResponseError(context, new Exception("The type is not a IAjaxProxy: " + className)); return; } try { IAjaxProxy ajax = (IAjaxProxy)Activator.CreateInstance(t, new object[] { }); QueryParams paras = new QueryParams(); if (!string.IsNullOrEmpty(context.Request.Params["page"])) { paras.PageIndex = Convert.ToInt32(context.Request.Params["page"]) - 1; } if (!string.IsNullOrEmpty(context.Request.Params["start"])) { paras.Start = Convert.ToInt32(context.Request.Params["start"]); } if (!string.IsNullOrEmpty(context.Request.Params["limit"])) { paras.PageSize = Convert.ToInt32(context.Request.Params["limit"]); } paras.Id = context.Request.Params["id"]; if (!string.IsNullOrEmpty(context.Request.Params["sort"])) { ExtSortParam para = new ExtSortParam(); para.Sorts = (ExtSort[])JavaScriptDeserializer.DeserializeFromJson( context.Request.Params["sort"], typeof(ExtSort[])); paras.Sort = para; } if (!string.IsNullOrEmpty(context.Request.Params["group"])) { ExtSortParam para = new ExtSortParam(); para.Sorts = (ExtSort[])JavaScriptDeserializer.DeserializeFromJson( context.Request.Params["group"], typeof(ExtSort[])); paras.Group = para; } if (!string.IsNullOrEmpty(context.Request.Params["filter"])) { ExtFilterParam para = new ExtFilterParam(); para.Filters = (ExtFilter[])JavaScriptDeserializer.DeserializeFromJson( context.Request.Params["filter"], typeof(ExtFilter[])); paras.Filter = para; } paras.Context = context; ajax.Query(paras); if (paras.RootDirect) { ResponseObject(context, paras.Results); } else { if (paras.Extra == null) { ResponseObject(context, new { success = true, total = paras.TotalRecords, root = paras.Results }); } else { ResponseObject(context, new { success = true, total = paras.TotalRecords, root = paras.Results, extra = paras.Extra }); } } } catch (Exception ex) { ResponseError(context, ex); return; } } #endregion #region POST/PUT/DELETE via IRestProxy if (context.Request.HttpMethod == "POST" || context.Request.HttpMethod == "PUT" || context.Request.HttpMethod == "DELETE") { if (!typeof(IRestProxy).IsAssignableFrom(t)) { ResponseError(context, new Exception("The type is not a IRestProxy: " + className)); return; } try { IRestProxy rest = (IRestProxy)Activator.CreateInstance(t, new object[] { }); string payload = ReadRequestPayload(context); object entity = JavaScriptDeserializer.DeserializeFromJson(payload, rest.EntityType); switch (context.Request.HttpMethod) { case "POST": //new object obj1 = rest.Create(entity); ResponseObject(context, new { success = true, root = new object[] { obj1 } }); break; case "PUT": //update object obj2 = rest.Update(entity); ResponseObject(context, new { success = true, root = new object[] { obj2 } }); break; case "DELETE": rest.Delete(entity); ResponseObject(context, new { success = true }); break; } } catch (Exception ex) { ResponseError(context, ex); return; } } #endregion }