AuthenticationResult GetAccessToken() { AuthenticationResult _authenticationResult = null; string resource = ConfigurationManager.AppSettings["ServiceRealm"]; try { if (selectedIdentityProviderDescriptor == null) { // Get the list of Idps List <IdentityProviderDescriptor> idps = (List <IdentityProviderDescriptor>)_authenticationContext.GetProviders(resource); if (idps.Count > 1) { // pop up a Home Realm Discovery window and let the user choose an Idp ChooseIdp(idps); } else { selectedIdentityProviderDescriptor = idps[0]; } } // Invoke AuthenticationContext.AcquireToken to obtain an access token to access the Shipper service. // It will use a pop-up window to initiate the logon flow. _authenticationResult = _authenticationContext.AcquireToken(resource, selectedIdentityProviderDescriptor); ClearErrorLabel(); if (_authenticationResult == null) { DisplayUserNotAuthenticatedError(); } } catch (ActiveDirectoryAuthenticationException ex) { string message = ex.Message; if (ex.InnerException != null) { message += (" " + ex.InnerException.Message); } DisplayErrorMessage(message); } catch (Exception ex) { DisplayErrorMessage(ex.Message); } return(_authenticationResult); }
private void ChooseIdp(List <IdentityProviderDescriptor> idps) { List <Button> list = new List <Button>(); // create a Button for each Idp foreach (IdentityProviderDescriptor idp in idps) { Button b = new Button(); b.Content = idp.Name; b.Click += button_Click; list.Add(b); } hrdPage = new HRD(); // Add the buttons to the Home Realm Discovery window hrdPage.AddButtons(list); // pop up the Home Realm Discovery window hrdPage.ShowDialog(); // select the Idp based on the Button clicked by the user selectedIdentityProviderDescriptor = idps.First(idp => idp.Name.Equals(this.selectedIdp)); }
internal static AssertionCredential AcquireToken(this Microsoft.WindowsAzure.ActiveDirectory.Authentication.AuthenticationContext self, Uri targetService, IdentityProviderDescriptor identityProvider, Credential credential) { return(self.AcquireToken(targetService.ToString(), identityProvider, credential)); }
ShipmentAsyncResult GetResponseFromService(string httpRequestMethod, string authorizationHeader, Shipment shipment = null) { string shipperServiceUrl = ConfigurationManager.AppSettings["TargetService"] + RelativePath; JavaScriptSerializer serializer = new JavaScriptSerializer(); IList <Shipment> shipmentList = null; try { HttpWebRequest request = WebRequest.Create(shipperServiceUrl) as HttpWebRequest; request.Method = httpRequestMethod; request.Headers["Authorization"] = authorizationHeader; request.ContentType = "application/json"; if (shipment != null) { // serialize the shipment if available string shipmentData; shipmentData = serializer.Serialize(shipment); using (Stream stream = request.GetRequestStream()) { stream.Write(Encoding.UTF8.GetBytes(shipmentData), 0, shipmentData.Length); } } using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // deserialize the response shipmentList = serializer.Deserialize( new StreamReader(response.GetResponseStream()).ReadToEnd(), typeof(IList <Shipment>)) as IList <Shipment>; response.Close(); } } catch (WebException webEx) { // error during fetching data string errorMessage = errorMessage = webEx.Message; if (webEx.Response != null) { HttpWebResponse response = (HttpWebResponse)webEx.Response; if (response.StatusCode == HttpStatusCode.Unauthorized) { MessageBoxResult result = MessageBox.Show("Do you want to clear the user from the token cache and reauthenticate?", "Error talking to service", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { // clear the cache and reauthenticate the user to get a new token _authenticationContext.TokenCacheStore.Clear(); selectedIdentityProviderDescriptor = null; DisplayErrorMessage("Please retry your operation now!"); } else { DisplayErrorMessage(errorMessage); } return(new ShipmentAsyncResult(null)); } else { errorMessage += new StreamReader(webEx.Response.GetResponseStream()).ReadToEnd(); throw new InvalidOperationException(errorMessage); } } } catch (Exception ex) { throw new InvalidOperationException("Error occurred while creating a new shipment. " + ex.Message); } return(new ShipmentAsyncResult(shipmentList)); }