private CswNbtWebServiceSessionCswNbtAuthReturn _Authenticate(NbtAuth auth, ServiceThreadEventArgs e, NbtPublicClient NbtClient) { CswNbtWebServiceSessionCswNbtAuthReturn ret = null; try { using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { ret = NbtClient.SessionInit(new CswWebSvcSessionAuthenticateDataAuthenticationRequest() { CustomerId = auth.AccessId, UserName = auth.UserId, Password = auth.Password, IsMobile = true, SuppressLog = true }); if (ret.Authentication.AuthenticationStatus == "Authenticated") { auth.sessionId = WebOperationContext.Current.IncomingResponse.Headers["X-NBT-SessionId"]; } else { e.Message += "Authentication error: " + ret.Authentication.AuthenticationStatus; } } } catch (Exception ex) { e.Message += "Authentication error: " + ex.Message; } finally { NbtClient.Close(); } return(ret); } // _Authenticate
/// <summary> /// Starts a new NBT session and performs the action specified. /// </summary> /// <param name="RequestCallback">The delegate to be executed when the authentication attempt succeeds or fails</param> public string PerformAction(SessionCompleteEvent RequestCallback) { string StatusText = ""; NbtPublicClient NbtClient = new NbtPublicClient(); try { string EndpointUrl = _formatUrl(baseURL); useSSL = (EndpointUrl.ToLower().IndexOf("https:") > -1); NbtClient.Endpoint.Address = new EndpointAddress(EndpointUrl); NbtClient.Endpoint.Binding = new WebHttpBinding { AllowCookies = true, Security = new WebHttpSecurity { Mode = useSSL ? WebHttpSecurityMode.Transport : WebHttpSecurityMode.None } }; //create a scope so that we can send the sessionId header using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { WebOperationContext.Current.OutgoingRequest.Headers.Add("X-NBT-SessionId", sessionId); StatusText = RequestCallback(NbtClient); } if (StatusText == "NonExistentSession") {//if the session doesn't exist, make an authentication request to get a new session ID //create a scope so we can extract the session id header using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { CswNbtWebServiceSessionCswNbtAuthReturn AuthenticationRequest = NbtClient.SessionInit(new CswWebSvcSessionAuthenticateDataAuthenticationRequest { CustomerId = AccessId, UserName = UserId, Password = Password, IsMobile = true, SuppressLog = true }); StatusText = AuthenticationRequest.Authentication.AuthenticationStatus; if (StatusText == "Authenticated") {//if the authentication was successful, re-send the web request sessionId = WebOperationContext.Current.IncomingResponse.Headers["X-NBT-SessionId"]; PerformAction(RequestCallback); } } //operationContextScope } //if the session doesn't exist } //try catch (Exception Ex) { StatusText += Ex.Message; } if (StatusText != "Authenticated") { announceBalanceTimer.Stop(); } NbtClient.Close(); return(StatusText); }//performAction()