1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
static void Main(string[] args)
{
string token = GetTokenFromACS("http://*****.cloudapp.net/Acs***/");
string serviceResponse = SendMessageToService(token);
Console.WriteLine("Service responded with: {0}\n", serviceResponse);
Console.WriteLine("Press to exit");
Console.ReadLine();
}
private static string SendMessageToService(string token)
{
WebClient client = new WebClient();
client.BaseAddress = ConfigurationManager.AppSettings.Get("ServiceAddress");
string headerValue = string.Format("WRAP access_token=\"{0}\"", token);
client.Headers.Add("Authorization", headerValue);
var serviceResponseBytes = client.DownloadString(String.Empty);
return serviceResponseBytes;
}
private static string GetTokenFromACS(string scope)
{
string wrapPassword = ConfigurationManager.AppSettings.Get("WrapPassword");
string wrapUsername = ConfigurationManager.AppSettings.Get("WrapUsername");
// request a token from ACS
WebClient client = new WebClient();
client.BaseAddress = string.Format("https://{0}.{1}", accessControlNamespace, accessControlHostName);
NameValueCollection values = new NameValueCollection();
values.Add("wrap_name", wrapUsername);
values.Add("wrap_password", wrapPassword);
values.Add("wrap_scope", scope);
byte[] responseBytes = client.UploadValues("WRAPv0.9/", "POST", values);
string response = Encoding.UTF8.GetString(responseBytes);
Console.WriteLine("\nreceived token from ACS: {0}\n", response);
return HttpUtility.UrlDecode(
response
.Split('&')
.Single(value => value.StartsWith("wrap_access_token=", StringComparison.OrdinalIgnoreCase))
.Split('=')[1]);
} |
Partager