Advertisement
Guest User

Untitled

a guest
Jul 25th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Console.WriteLine(RequestToken());
  6.             Console.ReadLine();
  7.         }
  8.  
  9.         private static string RequestToken()
  10.         {
  11.             ServicePointManager.ServerCertificateValidationCallback +=
  12.             (sender, certificate, chain, sslPolicyErrors) => true;
  13.  
  14.             var client1 = new RestClient();
  15.             client1.Authenticator = new HttpBasicAuthenticator("username", "password");
  16.             var request = new RestRequest(@"https://mysts.issuer.url" + @"?realm=https://myacsaddress.accesscontrol.windows.net", Method.GET);
  17.              var response1 = client1.Execute(request);
  18.             var samlToken = response1.Content;
  19.             NameValueCollection oauthToken;
  20.  
  21.             oauthToken = GetOAuthToken(samlToken, "https://myacsaddress.accesscontrol.windows.net",
  22.                                              "http://127.0.0.1:7000/");
  23.  
  24.             var oauthAuthorizationHeader = string.Format("Wrap {0}", oauthToken["access_token"]);
  25.             var client3 = new RestClient();
  26.             var request3 = new RestRequest(@"http://127.0.0.1:7000/", Method.POST);
  27.             request3.AddHeader(HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded");
  28.             request3.AddBody(oauthAuthorizationHeader);
  29.             var response3 = client3.Execute(request3);
  30.             return response3.Content;
  31.         }
  32.  
  33.         private static NameValueCollection GetOAuthToken(string xmlSamlToken, string serviceEndpoint, string acsRelyingParty)
  34.         {
  35.             var values = new NameValueCollection
  36.                              {
  37.                                  { "grant_type", "urn:oasis:names:tc:SAML:2.0:assertion" },
  38.                                  { "assertion", xmlSamlToken },
  39.                                  { "scope", acsRelyingParty }
  40.  
  41.                              };
  42.             var client = new WebClient { BaseAddress = serviceEndpoint };
  43.             byte[] acsTokenResponse = client.UploadValues("v2/OAuth2-13", "POST", values);
  44.             string acsToken = Encoding.UTF8.GetString(acsTokenResponse);
  45.             var tokens = new NameValueCollection();
  46.             var json = new JavaScriptSerializer();
  47.             var parsed = json.DeserializeObject(acsToken) as Dictionary<string, object>;
  48.             foreach (var item in parsed)
  49.             {
  50.                 tokens.Add(item.Key, item.Value.ToString());
  51.             }
  52.  
  53.             return tokens;
  54.         }
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement