/// <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>
 /// EndTwoWayAuthentication method implmentation
 /// </summary>
 public EndTwoWayAuthenticationResponse EndTwoWayAuthentication(EndTwoWayAuthenticationRequest request)
 {
     return(CreateMessage <EndTwoWayAuthenticationRequest, EndTwoWayAuthenticationResponse>(string.Format(CultureInfo.InvariantCulture, "{0}/{1}", new object[] { this._serviceUrl, "EndTwoWayAuthentication" }), request, new Action <HttpWebRequest>(this.SetAuthenticationHeader)));
 }