Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine(RequestToken());
- Console.ReadLine();
- }
- private static string RequestToken()
- {
- ServicePointManager.ServerCertificateValidationCallback +=
- (sender, certificate, chain, sslPolicyErrors) => true;
- var client1 = new RestClient();
- client1.Authenticator = new HttpBasicAuthenticator("username", "password");
- var request = new RestRequest(@"https://mysts.issuer.url" + @"?realm=https://myacsaddress.accesscontrol.windows.net", Method.GET);
- var response1 = client1.Execute(request);
- var samlToken = response1.Content;
- NameValueCollection oauthToken;
- oauthToken = GetOAuthToken(samlToken, "https://myacsaddress.accesscontrol.windows.net",
- "http://127.0.0.1:7000/");
- var oauthAuthorizationHeader = string.Format("Wrap {0}", oauthToken["access_token"]);
- var client3 = new RestClient();
- var request3 = new RestRequest(@"http://127.0.0.1:7000/", Method.POST);
- request3.AddHeader(HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded");
- request3.AddBody(oauthAuthorizationHeader);
- var response3 = client3.Execute(request3);
- return response3.Content;
- }
- private static NameValueCollection GetOAuthToken(string xmlSamlToken, string serviceEndpoint, string acsRelyingParty)
- {
- var values = new NameValueCollection
- {
- { "grant_type", "urn:oasis:names:tc:SAML:2.0:assertion" },
- { "assertion", xmlSamlToken },
- { "scope", acsRelyingParty }
- };
- var client = new WebClient { BaseAddress = serviceEndpoint };
- byte[] acsTokenResponse = client.UploadValues("v2/OAuth2-13", "POST", values);
- string acsToken = Encoding.UTF8.GetString(acsTokenResponse);
- var tokens = new NameValueCollection();
- var json = new JavaScriptSerializer();
- var parsed = json.DeserializeObject(acsToken) as Dictionary<string, object>;
- foreach (var item in parsed)
- {
- tokens.Add(item.Key, item.Value.ToString());
- }
- return tokens;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement