/// <summary> /// Creates a new instance of credentials to use in authentication process. /// </summary> /// <param name="ContextIdentifier">The PublicClientApplicationID or EnvironmentUniqiueID.</param> /// <param name="ClientID">Username.</param> /// <param name="ClientSecret">Password.</param> /// <param name="AuthenticationType">Authentication Type.</param> internal Credentials(System.Guid ContextIdentifier, System.String ClientID, System.String ClientSecret, SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes AuthenticationType) { this.CredentialsContext = this; this.CredentialsContext.ContextIdentifier = ContextIdentifier; this.CredentialsContext.ClientID = ClientID; this.CredentialsContext.ClientSecret = ClientSecret; this.CredentialsContext.AuthenticationType = AuthenticationType; }
/// <summary> /// Clears the authentication objects and cache data. /// </summary> public static void SignOut() { SoftmakeAll.SDK.Fluent.GeneralCacheHelper.Clear(); SoftmakeAll.SDK.Fluent.SDKContext.ClientWebSocket.DisposeAsync().ConfigureAwait(false); SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials = null; SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult = null; SoftmakeAll.SDK.Fluent.SDKContext.PublicClientApplication = null; }
/// <summary> /// Authenticate user/application using Credentials. /// </summary> /// <param name="Credentials">Credentials to use during authentication process.</param> public static async System.Threading.Tasks.Task AuthenticateAsync(SoftmakeAll.SDK.Fluent.Authentication.ICredentials Credentials) { if (Credentials != null) { SoftmakeAll.SDK.Fluent.SDKContext.SignOut(); SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials = Credentials; // From AccessKey if (Credentials.AuthenticationType == SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes.Application) { SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.Authorization = $"Basic {System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{Credentials.ClientID}@{Credentials.ContextIdentifier.ToString().ToLower()}:{Credentials.ClientSecret}"))}"; SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.Store(); await SoftmakeAll.SDK.Fluent.SDKContext.ClientWebSocket.ConfigureAsync(SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.Authorization); return; } } else if (SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials == null) { try { System.Text.Json.JsonElement CacheData = SoftmakeAll.SDK.Fluent.GeneralCacheHelper.ReadString().ToJsonElement(); if (!(CacheData.IsValid())) { throw new System.Exception(); } // From AccessKey if (CacheData.GetInt32("AuthType") == (int)SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes.Application) { SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials = new SoftmakeAll.SDK.Fluent.Authentication.Credentials(CacheData.GetGuid("ContextIdentifier"), CacheData.GetString("ClientID"), null, (SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes)CacheData.GetInt32("AuthType")); SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.Authorization = CacheData.GetString("Authorization"); if (System.String.IsNullOrWhiteSpace(SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.Authorization)) { throw new System.Exception(); } await SoftmakeAll.SDK.Fluent.SDKContext.ClientWebSocket.ConfigureAsync(SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.Authorization); return; } else { SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials = new SoftmakeAll.SDK.Fluent.Authentication.Credentials(CacheData.GetJsonElement("AppMetadata").EnumerateObject().First().Value.GetGuid("client_id")); SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.AuthenticationType = SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes.Interactive; } } catch { } } if (SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials == null) { SoftmakeAll.SDK.Fluent.SDKContext.SignOut(); throw new System.Exception("Invalid Credentials from cache."); } // From AccessKey if (SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.AuthenticationType == SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes.Application) { return; } // From Public Client Application if ((SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult == null) || (SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult.ExpiresOn.Subtract(System.DateTimeOffset.UtcNow).TotalMinutes <= 5.0D)) { System.String[] Scopes = new System.String[] { "openid", "https://softmakeb2c.onmicrosoft.com/48512da7-b030-4e62-be61-9e19b2c52d8a/user_impersonation" }; if (SoftmakeAll.SDK.Fluent.SDKContext.PublicClientApplication == null) { if (SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.AuthenticationType == SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes.Interactive) // From Interactive { SoftmakeAll.SDK.Fluent.SDKContext.PublicClientApplication = SoftmakeAll.SDK.Fluent.SDKContext.CreatePublicClientApplication(SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.ContextIdentifier, "A_signup_signin", "http://localhost:1435"); } else if (SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.AuthenticationType == SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes.Credentials) // From Username and Password { SoftmakeAll.SDK.Fluent.SDKContext.PublicClientApplication = SoftmakeAll.SDK.Fluent.SDKContext.CreatePublicClientApplication(SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.ContextIdentifier, "_ROPC"); } else { throw new System.Exception("Invalid authentication type."); } } // Getting existing Account in cache try { System.Collections.Generic.IEnumerable <Microsoft.Identity.Client.IAccount> Accounts = await SoftmakeAll.SDK.Fluent.SDKContext.PublicClientApplication.GetAccountsAsync(); if (Accounts.Any()) { SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult = await SoftmakeAll.SDK.Fluent.SDKContext.PublicClientApplication.AcquireTokenSilent(Scopes, Accounts.FirstOrDefault()).ExecuteAsync(); if (SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult != null) { SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.Authorization = $"Bearer {SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult.AccessToken}"; await SoftmakeAll.SDK.Fluent.SDKContext.ClientWebSocket.ConfigureAsync(SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult.AccessToken); return; } } } catch { SoftmakeAll.SDK.Fluent.GeneralCacheHelper.Clear(); } if (SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.AuthenticationType == SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes.Interactive) // From Interactive { try { SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult = await SoftmakeAll.SDK.Fluent.SDKContext.PublicClientApplication.AcquireTokenInteractive(Scopes).WithPrompt(Microsoft.Identity.Client.Prompt.ForceLogin).ExecuteAsync(); } catch { } } else if (SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.AuthenticationType == SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes.Credentials) // From Username and Password { if (System.String.IsNullOrWhiteSpace(SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.ClientSecret)) { SoftmakeAll.SDK.Fluent.SDKContext.SignOut(); throw new System.Exception("Authentication aborted. Please, re-enter credentials."); } System.Security.SecureString Password = new System.Security.SecureString(); foreach (System.Char Char in SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.ClientSecret) { Password.AppendChar(Char); } Password.MakeReadOnly(); try { SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult = await SoftmakeAll.SDK.Fluent.SDKContext.PublicClientApplication.AcquireTokenByUsernamePassword(Scopes, SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.ClientID, Password).ExecuteAsync(); Password.Dispose(); } catch { Password.Dispose(); SoftmakeAll.SDK.Fluent.SDKContext.SignOut(); throw new System.Exception("Invalid username or password."); } } if (SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult == null) { SoftmakeAll.SDK.Fluent.SDKContext.SignOut(); throw new System.Exception("Authentication aborted."); } SoftmakeAll.SDK.Fluent.SDKContext.InMemoryCredentials.Authorization = $"Bearer {SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult.AccessToken}"; await SoftmakeAll.SDK.Fluent.SDKContext.ClientWebSocket.ConfigureAsync(SoftmakeAll.SDK.Fluent.SDKContext.AuthenticationResult.AccessToken); return; } }
/// <summary> /// Authenticate user/application using Credentials. /// </summary> /// <param name="Credentials">Credentials to use during authentication process.</param> public static void Authenticate(SoftmakeAll.SDK.Fluent.Authentication.ICredentials Credentials) => SoftmakeAll.SDK.Fluent.SDKContext.AuthenticateAsync(Credentials).Wait();
/// <summary> /// Creates a new instance of credentials to use in authentication process. /// </summary> /// <param name="ContextIdentifier">The PublicClientApplicationID or EnvironmentUniqiueID.</param> internal Credentials(System.Guid ContextIdentifier) { this.CredentialsContext = this; this.CredentialsContext.ContextIdentifier = ContextIdentifier; this.CredentialsContext.AuthenticationType = SoftmakeAll.SDK.Fluent.Authentication.AuthenticationTypes.Interactive; }