/// <summary> /// Retrieves an authorization token from the server /// </summary> /// <param name="portalUrl">URL of the Cireson Portal</param> /// <param name="userName">User name</param> /// <param name="password">User's password</param> /// <param name="languageCode">Portal language code</param> /// <exception cref="System.Security.Authentication.InvalidCredentialException">Thrown when user credentials are invalid.</exception> /// <exception cref="CiresonPortalAPI.CiresonApiException">Thrown when an error occurs in the API.</exception> /// <returns></returns> public static async Task <AuthorizationToken> GetAuthorizationToken(string portalUrl, string userName, SecureString password, string domain, string languageCode = "ENU") { try { // First check to see if we have Windows Authentication enabled bool windowsAuthEnabled = await DetectWindowsAuthentication(portalUrl); // Set up credentials PortalCredentials credentials = new PortalCredentials(); credentials.Username = userName; credentials.SecurePassword = password; credentials.Domain = domain; // Initialize the HTTP helper and do the heavy lifting PortalHttpHelper helper = new PortalHttpHelper(portalUrl, credentials, windowsAuthEnabled); string result = await helper.PostAsync(AUTHORIZATION_ENDPOINT, JsonConvert.SerializeObject(new { UserName = credentials.Username, Password = credentials.Password, LanguageCode = languageCode })); // Strip off beginning and ending quotes result = result.TrimStart('\"').TrimEnd('\"'); // Create a new authorization token AuthorizationToken token = new AuthorizationToken(portalUrl, credentials, languageCode, result, windowsAuthEnabled); // Fetch this user's properties ConsoleUser user = await AuthorizationController.GetUserRights(token, userName, domain); token.User = user; return(token); } catch { throw; // Rethrow exceptions } }
/// <summary> /// Retrieves an authorization token from the server /// </summary> /// <param name="portalUrl">URL of the Cireson Portal</param> /// <param name="userName">User name</param> /// <param name="password">User's password</param> /// <param name="languageCode">Portal language code</param> /// <exception cref="System.Security.Authentication.InvalidCredentialException">Thrown when user credentials are invalid.</exception> /// <exception cref="CiresonPortalAPI.CiresonApiException">Thrown when an error occurs in the API.</exception> /// <returns></returns> public static async Task<AuthorizationToken> GetAuthorizationToken(string portalUrl, string userName, SecureString password, string languageCode = "ENU") { try { // First check to see if we have Windows Authentication enabled bool windowsAuthEnabled = await DetectWindowsAuthentication(portalUrl); // Set up credentials PortalCredentials credentials = new PortalCredentials(); credentials.Username = userName; credentials.SecurePassword = password; // Initialize the HTTP helper and do the heavy lifting PortalHttpHelper helper = new PortalHttpHelper(portalUrl, credentials, windowsAuthEnabled); string result = await helper.PostAsync(AUTHORIZATION_ENDPOINT, JsonConvert.SerializeObject(new { UserName = credentials.Username, Password = credentials.Password, LanguageCode = languageCode })); // Strip off beginning and ending quotes result = result.TrimStart('\"').TrimEnd('\"'); // Return the authorization token return new AuthorizationToken(portalUrl, credentials, languageCode, result, windowsAuthEnabled); } catch { throw; // Rethrow exceptions } }
/// <summary> /// Constructor /// </summary> /// <param name="portalUrl">URL to access</param> /// <param name="credentials">Credentials</param> /// <param name="languageCode">Language code of the Portal</param> /// <param name="token">Authorization token</param> /// <param name="windowsAuthEnabled">Is Windows Authentication enabled?</param> internal AuthorizationToken(string portalUrl, PortalCredentials credentials, string languageCode, string token, bool windowsAuthEnabled) { _oCredentials = credentials; _sLanguageCode = languageCode; _sToken = token; _sPortalUrl = portalUrl; _dExpirationTime = DateTime.Now.AddSeconds(EXPIRATION_SECONDS); _bWindowsAuthEnabled = windowsAuthEnabled; }
/// <summary> /// Default constructor, no Authorization header; should only be used with the /Authorization/GetToken endpoint /// </summary> /// <param name="portalUrl">URL of the Cireson Portal</param> /// <param name="credentials">PortalCredentials object</param> /// <param name="windowsAuthEnabled">Does the portal have Windows Authentication enabled?</param> public PortalHttpHelper(string portalUrl, PortalCredentials credentials, bool windowsAuthEnabled) { // Create the HttpClientHandler HttpClientHandler handler = new HttpClientHandler(); // Add credentials to the client handler if we're using Windows Auth if (windowsAuthEnabled) { // Create the network credential cache and add it to the handler CredentialCache cache = new CredentialCache(); cache.Add(new Uri(portalUrl), "Negotiate", new NetworkCredential(credentials.Username, credentials.SecurePassword, credentials.Domain)); handler.Credentials = cache; } // Create the HTTP client _oHttpClient = new HttpClient(handler); _oHttpClient.BaseAddress = new Uri(portalUrl); _oHttpClient.DefaultRequestHeaders.Add("Accept", "application/json"); }