/// <summary>
        /// GetAzureAvailableAuthenticationMethods method implementation
        /// </summary>
        private GetAvailableAuthenticationMethodsResponse GetAzureAvailableAuthenticationMethods(AuthenticationContext ctx)
        {
            if (ctx == null)
            {
                throw new ArgumentNullException("ctx");
            }
            if (string.IsNullOrEmpty(ctx.UPN))
            {
                throw new InvalidDataException("No user identity was provided.");
            }

            GetAvailableAuthenticationMethodsRequest request = new GetAvailableAuthenticationMethodsRequest()
            {
                UserPrincipalName = ctx.UPN, ContextId = ctx.ActivityId
            };
            GetAvailableAuthenticationMethodsResponse response;

            try
            {
                response = SasProvider.GetAvailableAuthenticationMethods(request);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception calling SAS.", ex);
            }

            if (response.Result.Value != "Success")
            {
                throw new Exception(string.Format("Unexpected SAS response status code : {0}", response.Result.Value));
            }
            return(response);
        }
        /// <summary>
        /// EndAzureAuthentication method implementation
        /// </summary>
        private AuthenticationResponseKind EndAzureAuthentication(AuthenticationContext ctx, string code)
        {
            if (ctx == null)
            {
                throw new ArgumentNullException("authContext");
            }
            if (string.IsNullOrEmpty(ctx.UPN))
            {
                throw new InvalidDataException("No user identity was provided.");
            }

            EndTwoWayAuthenticationRequest request = new EndTwoWayAuthenticationRequest
            {
                ContextId         = ctx.ActivityId,
                SessionId         = ctx.SessionId,
                UserPrincipalName = ctx.UPN,
            };

            if (!string.IsNullOrEmpty(code))
            {
                request.AdditionalAuthData = code;
            }
            EndTwoWayAuthenticationResponse response;

            try
            {
                do
                {
                    response = SasProvider.EndTwoWayAuthentication(request);
                    if (response.Result.Value.Equals("AuthenticationPending"))
                    {
                        Thread.Sleep(1000);
                    }
                } while (response.Result.Value.Equals("AuthenticationPending"));
            }
            catch (Exception ex)
            {
                throw new Exception("Exception calling SAS.", ex);
            }
            if (response.Result.Value != "Success")
            {
                return(AuthenticationResponseKind.Error);
            }
            return(ctx.SelectedMethod);
        }
        /// <summary>
        /// BeginAzureAuthentication method implementation
        /// </summary>
        private AuthenticationResponseKind BeginAzureAuthentication(AuthenticationContext ctx)
        {
            if (ctx == null)
            {
                throw new ArgumentNullException("authContext");
            }
            if (string.IsNullOrEmpty(ctx.UPN))
            {
                throw new InvalidDataException("No user identity was provided.");
            }

            BeginTwoWayAuthenticationRequest request = new BeginTwoWayAuthenticationRequest
            {
                Lcid = CultureInfo.GetCultureInfo(ctx.Lcid).Name,
                UserPrincipalName = ctx.UPN,

                CompanyName            = this.CompanyName,
                AuthenticationMethodId = AuthenticationMethodToString(ctx.SelectedMethod),
                ReplicationScope       = null,
                ContextId = ctx.ActivityId,
            };

            BeginTwoWayAuthenticationResponse response;

            try
            {
                response = SasProvider.BeginTwoWayAuthentication(request);
            }
            catch (Exception ex2)
            {
                throw new Exception("Exception calling SAS.", ex2);
            }
            if (response.Result.Value != "Success")
            {
                return(AuthenticationResponseKind.Error);
            }
            ctx.SessionId = response.SessionId;
            return(ctx.SelectedMethod);
        }