/// <summary> /// Sets the request header namespace in outgoing Soap Requests. /// </summary> /// <param name="signature">The service creation parameters.</param> /// <param name="service">The service object for which RequestHeader /// needs to be patched.</param> private static void SetRequestHeaderNameSpace(DfaServiceSignature signature, AdsClient service) { // Set the header namespace prefix. For all /cm services, the members // shouldn't have xmlns. For all other services, the members should have // /cm as xmlns. object[] attributes = service.GetType().GetCustomAttributes(false); foreach (object attribute in attributes) { if (attribute is WebServiceBindingAttribute) { WebServiceBindingAttribute binding = (WebServiceBindingAttribute)attribute; string xmlns = binding.Namespace; RequestHeader svcRequestHeader = null; PropertyInfo propInfo = service.GetType().GetProperty("RequestHeader"); if (propInfo != null) { svcRequestHeader = (RequestHeader)propInfo.GetValue(service, null); if (svcRequestHeader != null) { PropertyInfo wsPropInfo = svcRequestHeader.GetType().GetProperty("TargetNamespace"); if (wsPropInfo != null) { wsPropInfo.SetValue(svcRequestHeader, xmlns, null); } } } } } }
/// <summary> /// Create a service object. /// </summary> /// <param name="signature">Signature of the service being created.</param> /// <param name="user">The user for which the service is being created. /// <param name="serverUrl">The server to which the API calls should be /// made.</param> /// </param> /// <returns>An object of the desired service type.</returns> public override AdsClient CreateService(ServiceSignature signature, AdsUser user, Uri serverUrl) { DfaAppConfig config = (DfaAppConfig)base.Config; if (serverUrl == null) { serverUrl = new Uri(config.DfaApiServer); } if (user == null) { throw new ArgumentNullException("user"); } CheckServicePreconditions(signature); DfaServiceSignature dfaapiSignature = signature as DfaServiceSignature; AdsClient service = (AdsClient)Activator.CreateInstance(dfaapiSignature.ServiceType); if (config.Proxy != null) { service.Proxy = config.Proxy; } service.Timeout = config.Timeout; service.Url = string.Format("{0}{1}/api/dfa-api/{2}", serverUrl, dfaapiSignature.Version, dfaapiSignature.ServiceEndpoint); service.UserAgent = config.GetUserAgent(); service.User = user; service.Signature = signature; service.GetType().GetProperty("RequestHeader").SetValue(service, GetRequestHeader(), null); SetRequestHeaderNameSpace(signature as DfaServiceSignature, service); return(service); }
/// <summary> /// Generates a login token for authenticating DFA API calls. /// </summary> /// <param name="user">The user for which token is generated.</param> /// <param name="serviceVersion">The service version.</param> /// <returns>A token which may be used for future API calls.</returns> private static UserToken GenerateAuthenticationToken(AdsUser user, string serviceVersion) { DfaAppConfig config = (DfaAppConfig) user.Config; if (!String.IsNullOrEmpty(config.DfaAuthToken)) { return new UserToken(config.DfaUserName, config.DfaAuthToken); } String dfaUserName = config.DfaUserName; String dfaPassword = config.DfaPassword; if (config.AuthorizationMethod == DfaAuthorizationMethod.LoginService) { if (string.IsNullOrEmpty(dfaUserName)) { throw new ArgumentNullException(DfaErrorMessages.UserNameCannotBeEmpty); } if (string.IsNullOrEmpty(dfaPassword)) { throw new ArgumentNullException(DfaErrorMessages.PasswordCannotBeEmpty); } } else if (config.AuthorizationMethod == DfaAuthorizationMethod.OAuth2) { // DFA password should not be set when using OAuth2 dfaPassword = ""; } try { DfaServiceSignature loginServiceSignature = new DfaServiceSignature(serviceVersion, "LoginRemoteService"); AdsClient loginService = user.GetService(loginServiceSignature, config.DfaApiServer); object userProfile = loginService.GetType().GetMethod("authenticate").Invoke( loginService, new object[] {dfaUserName, dfaPassword}); return new UserToken( userProfile.GetType().GetProperty("name").GetValue(userProfile, null).ToString(), userProfile.GetType().GetProperty("token").GetValue(userProfile, null).ToString()); } catch (Exception e) { throw new DfaException("Failed to authenticate user. See inner exception for details.", e); } }
/// <summary> /// Sets the request header namespace in outgoing Soap Requests. /// </summary> /// <param name="signature">The service creation parameters.</param> /// <param name="service">The service object for which RequestHeader /// needs to be patched.</param> private static void SetRequestHeaderNameSpace(DfaServiceSignature signature, AdsClient service) { // Set the header namespace prefix. For all /cm services, the members // shouldn't have xmlns. For all other services, the members should have // /cm as xmlns. object[] attributes = service.GetType().GetCustomAttributes(false); foreach (object attribute in attributes) { if (attribute is WebServiceBindingAttribute) { WebServiceBindingAttribute binding = (WebServiceBindingAttribute) attribute; string xmlns = binding.Namespace; RequestHeader svcRequestHeader = null; PropertyInfo propInfo = service.GetType().GetProperty("RequestHeader"); if (propInfo != null) { svcRequestHeader = (RequestHeader) propInfo.GetValue(service, null); if (svcRequestHeader != null) { PropertyInfo wsPropInfo = svcRequestHeader.GetType().GetProperty("TargetNamespace"); if (wsPropInfo != null) { wsPropInfo.SetValue(svcRequestHeader, xmlns, null); } } } } } }