/// <summary> /// Creates authentication parameters from the WWW-Authenticate header in response received from resource. This method expects the header to contain authentication parameters. /// </summary> /// <param name="authenticateHeader">Content of header WWW-Authenticate header</param> /// <returns>AuthenticationParameters object containing authentication parameters</returns> public static AuthenticationParameters CreateFromResponseAuthenticateHeader(string authenticateHeader) { if (string.IsNullOrWhiteSpace(authenticateHeader)) { throw new ArgumentNullException("authenticateHeader"); } authenticateHeader = authenticateHeader.Trim(); // This also checks for cases like "BearerXXXX authorization_uri=...." and "Bearer" and "Bearer " if (!authenticateHeader.StartsWith(Bearer, StringComparison.OrdinalIgnoreCase) || authenticateHeader.Length < Bearer.Length + 2 || !char.IsWhiteSpace(authenticateHeader[Bearer.Length])) { var ex = new ArgumentException(AdalErrorMessage.InvalidAuthenticateHeaderFormat, "authenticateHeader"); Logger.Error(null, ex); throw ex; } authenticateHeader = authenticateHeader.Substring(Bearer.Length).Trim(); Dictionary<string, string> authenticateHeaderItems = EncodingHelper.ParseKeyValueList(authenticateHeader, ',', false, null); var authParams = new AuthenticationParameters(); string param; authenticateHeaderItems.TryGetValue(AuthorityKey, out param); authParams.Authority = param; authenticateHeaderItems.TryGetValue(ResourceKey, out param); authParams.Resource = param; return authParams; }
/// <summary> /// Creates authentication parameters from the WWW-Authenticate header in response received from resource. This method expects the header to contain authentication parameters. /// </summary> /// <param name="authenticateHeader">Content of header WWW-Authenticate header</param> /// <returns>AuthenticationParameters object containing authentication parameters</returns> public static AuthenticationParameters CreateFromResponseAuthenticateHeader(string authenticateHeader) { if (string.IsNullOrWhiteSpace(authenticateHeader)) { throw new ArgumentNullException("authenticateHeader"); } authenticateHeader = authenticateHeader.Trim(); // This also checks for cases like "BearerXXXX authorization_uri=...." and "Bearer" and "Bearer " if (!authenticateHeader.StartsWith(Bearer, StringComparison.OrdinalIgnoreCase) || authenticateHeader.Length < Bearer.Length + 2 || !char.IsWhiteSpace(authenticateHeader[Bearer.Length])) { var ex = new ArgumentException(AdalErrorMessage.InvalidAuthenticateHeaderFormat, nameof(authenticateHeader)); PlatformPlugin.Logger.Error(null, ex); throw ex; } authenticateHeader = authenticateHeader.Substring(Bearer.Length).Trim(); IDictionary <string, string> authenticateHeaderItems; try { authenticateHeaderItems = EncodingHelper.ParseKeyValueListStrict(authenticateHeader, ',', false, true, null); } catch (ArgumentException ex) { var newEx = new ArgumentException(AdalErrorMessage.InvalidAuthenticateHeaderFormat, nameof(authenticateHeader), ex); PlatformPlugin.Logger.Error(null, newEx); throw newEx; } var authParams = new AuthenticationParameters(); string param; authenticateHeaderItems.TryGetValue(AuthorityKey, out param); authParams.Authority = param; authenticateHeaderItems.TryGetValue(ResourceKey, out param); authParams.Resource = param; return(authParams); }
public bool GetCRMConnection() { Credentials = new ClientCredentials(); try { XmlTextReader reader = new XmlTextReader("Connection.xml"); string element = ""; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { element = reader.Name; } else if (reader.NodeType == XmlNodeType.Text) { switch (element) { case "userlicence": //Display the text in each element. bool isValid = Guid.TryParse(reader.Value, out license); if (!isValid) { MessageBox.Show("The license is in an invalid format. Time Tracker will not be able to sycn to JARVIS"); return(false); } break; } } } reader.Close(); string organizationUrl = "https://csp.crm.dynamics.com"; string resourceURL = "https://csp.api.crm.dynamics.com" + "/api/data/"; string clientId = "c4e4407b-66d1-4452-9b05-db0a0ce9baef"; // Client Id string appKey = "Sy[?Mk106C2OvHXZ:Krytwj=_XN_KKlh"; //Client Secret //Create the Client credentials to pass for authentication Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential clientcred = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, appKey); //get the authentication parameters Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationParameters authParam = Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(resourceURL)).Result; //Generate the authentication context - this is the azure login url specific to the tenant string authority = authParam.Authority; //request token AuthenticationResult authenticationResult = new AuthenticationContext(authority).AcquireTokenAsync(organizationUrl, clientcred).Result; //get the token string token = authenticationResult.AccessToken; Uri serviceUrl = new Uri(organizationUrl + @"/xrmservices/2011/organization.svc/web?SdkClientVersion=9.1"); OrganizationWebProxyClient sdkService; sdkService = new OrganizationWebProxyClient(serviceUrl, false); sdkService.CallerId = license; sdkService.HeaderToken = token; _service = (Microsoft.Xrm.Sdk.IOrganizationService)sdkService != null ? (Microsoft.Xrm.Sdk.IOrganizationService)sdkService : null; Microsoft.Xrm.Sdk.Entity user = _service.Retrieve("systemuser", license, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)); return(true); } catch (Exception ex) { MessageBox.Show("You do not have a valid license for JARIVS and will not be able to sync your time"); return(false); } }