OK, 现在你可以通过启动Silverlight项目来调用Service了,先点击Click for Token 后点击Click for data.
我还创建一个控制台程序来调用它,效果是一样的:
- class Program
- {
- /// <summary>
- /// Necessary variables from ACS Portal.
- /// </summary>
- static string serviceNamespace = "<Your ACS namespace>";
- static string acsHostUrl = "accesscontrol.windows.net";
- static string realm = "<Your ACS service path>";
- static string userUrl = "<Your user service path>";
- static string uid = "<Your service identity name>";
- static string pwd = "<Your service identity password>";
- /// <summary>
- /// Access the service via ACS Token.
- /// </summary>
- /// <param name="args"></param>
- static void Main(string[] args)
- {
- string token = GetTokenFromACS(realm);
- WebClient client = new WebClient();
- string headerValue = string.Format("WRAP access_token=\"{0}\"", token);
- HttpWebRequest request = HttpWebRequest.Create(userUrl) as HttpWebRequest;
- request.ContentLength = 0;
- request.Method = "GET";
- request.Headers["Authorization"] = headerValue;
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string obj = reader.ReadToEnd();
- Console.Write(obj);
- Console.ReadLine();
- }
- }
- /// <summary>
- /// Get Token from ACS.
- /// </summary>
- /// <param name="scope"></param>
- /// <returns></returns>
- private static string GetTokenFromACS(string scope)
- {
- string wrapPassword = pwd;
- string wrapUsername = uid;
- // request a token from ACS
- WebClient client = new WebClient();
- client.BaseAddress = string.Format("https://{0}.{1}", serviceNamespace, acsHostUrl);
- 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]);
- }
- }
好了,你也可以试试运行一下这个控制台程序。
