/// <summary> /// Sets the authentication tokens. /// </summary> /// <param name="userToken">The user token.</param> /// <param name="deviceToken">The device token.</param> /// <returns>A task representing completion.</returns> public Task SetAuthenticationTokens(string userToken, string deviceToken) { CommerceRuntimeUserToken userIdToken = string.IsNullOrWhiteSpace(userToken) ? null : new CommerceRuntimeUserToken(userToken); CommerceRuntimeContext.Value.SetUserToken(userIdToken); CommerceRuntimeContext.Value.SetDeviceToken(deviceToken); return(Task.FromResult <object>(null)); }
/// <summary> /// Acquires the user token. /// </summary> /// <param name="userName">Name of the user.</param> /// <param name="password">The password of the user.</param> /// <param name="commerceAuthenticationParameters">The commerce authentication parameters.</param> /// <returns>The user commerce runtime token.</returns> internal override Task <UserToken> AcquireToken(string userName, string password, CommerceAuthenticationParameters commerceAuthenticationParameters) { ThrowIf.Null(commerceAuthenticationParameters, "commerceAuthenticationParameters"); return(Execute <UserToken>(() => { CommerceRuntimeUserToken commerceUserToken; CommerceIdentity originalIdentity = CommerceRuntimeManager.Identity; ConnectionRequest connectionRequest = this.CreateAcquireTokenRequest(userName, password); connectionRequest.Credential = commerceAuthenticationParameters.Credential; connectionRequest.GrantType = commerceAuthenticationParameters.GrantType; connectionRequest.AdditionalAuthenticationData = commerceAuthenticationParameters; LogonCredentials credentials = null; if (!commerceAuthenticationParameters.RetailOperation.HasValue) { try { CommerceIdentity commerceIdentity = new CommerceIdentity(string.Empty, 0, 0, new string[] { }); commerceIdentity.Roles.Add(CommerceRoles.Anonymous); // Set anonymous identity from request. CommerceRuntimeManager.Identity = commerceIdentity; credentials = SecurityManager.Create(CommerceRuntimeManager.Runtime).LogOn(connectionRequest); // Clear the commerce identity. CommerceRuntimeManager.Identity = null; } catch (Exception) { CommerceRuntimeManager.Identity = originalIdentity; throw; } commerceUserToken = new CommerceRuntimeUserToken(credentials.Identity); } else { credentials = SecurityManager.Create(CommerceRuntimeManager.Runtime).ElevateUser(connectionRequest, (RetailOperation)commerceAuthenticationParameters.RetailOperation); commerceUserToken = new CommerceRuntimeUserToken(originalIdentity, credentials.Identity); } return commerceUserToken; })); }
/// <summary> /// Gets the user token. /// </summary> /// <returns>The user token.</returns> internal UserToken GetUserToken() { UserToken onlineUserToken = this.OnlineContext.GetUserToken(); CommerceRuntimeUserToken offlineToken = this.OfflineContext.GetUserToken() as CommerceRuntimeUserToken; if (onlineUserToken == null) { return(offlineToken); } else if (onlineUserToken is UserIdToken) { CommerceUserToken commerceUserToken = new CommerceUserToken(onlineUserToken as UserIdToken); commerceUserToken.CommerceRuntimeToken = offlineToken; return(commerceUserToken); } else { return(onlineUserToken); } }
/// <summary> /// Sets the user identity. /// </summary> /// <param name="userToken">The user token.</param> internal static void SetUserIdentity(UserToken userToken) { ThrowIf.Null(userToken, "userToken"); CommerceUserToken commerceUserToken = userToken as CommerceUserToken; CommerceRuntimeUserToken runtimeToken = userToken as CommerceRuntimeUserToken; // A user token has to be either a commerce runtime token or a commerce user token which can contain a commerce runtime token, or retail server token or both. if (commerceUserToken == null && runtimeToken == null) { throw new RetailProxy.UserAuthenticationException(SecurityErrors.Microsoft_Dynamics_Commerce_Runtime_InvalidUserToken.ToString(), string.Format("Invalid token type provided {0}.", userToken.GetType())); } runtimeToken = commerceUserToken == null ? runtimeToken : commerceUserToken.CommerceRuntimeToken; if (runtimeToken == null) { throw new RetailProxy.UserAuthenticationException(SecurityErrors.Microsoft_Dynamics_Commerce_Runtime_InvalidUserToken.ToString(), string.Format("The provided commerce runtime token cannot be null.")); } Identity = Newtonsoft.Json.JsonConvert.DeserializeObject <CommerceIdentity>(runtimeToken.Token); }
/// <summary> /// Initializes a new instance of the <see cref="CommerceUserToken"/> class. /// </summary> /// <param name="commerceRuntimeToken">The user token that can be used to authenticate Commerce Runtime calls.</param> internal CommerceUserToken(CommerceRuntimeUserToken commerceRuntimeToken) : base(CommerceRuntimeUserToken.CommerceRuntimeTokenSchemeName) { ThrowIf.Null(commerceRuntimeToken, "commerceRuntimeToken"); this.CommerceRuntimeToken = commerceRuntimeToken; }