你好,游客 登录 注册 搜索
背景:
阅读新闻

使用OAuth方式保护你的WCF Service(通过Azure ACS)

[日期:2012-07-27] 来源:Linux社区  作者:aa466564931 [字体:大 中 小]

OK, 现在你可以通过启动Silverlight项目来调用Service了,先点击Click for Token 后点击Click for data.

我还创建一个控制台程序来调用它,效果是一样的:

  1. class Program  
  2. {  
  3.     /// <summary>   
  4.     /// Necessary variables from ACS Portal.   
  5.     /// </summary>   
  6.     static string serviceNamespace = "<Your ACS namespace>";  
  7.     static string acsHostUrl = "accesscontrol.windows.net";  
  8.     static string realm = "<Your ACS service path>";  
  9.     static string userUrl = "<Your user service path>";  
  10.     static string uid = "<Your service identity name>";  
  11.     static string pwd = "<Your service identity password>";  
  12.   
  13.     /// <summary>   
  14.     /// Access the service via ACS Token.   
  15.     /// </summary>   
  16.     /// <param name="args"></param>   
  17.     static void Main(string[] args)  
  18.     {  
  19.         string token = GetTokenFromACS(realm);  
  20.   
  21.         WebClient client = new WebClient();  
  22.   
  23.         string headerValue = string.Format("WRAP access_token=\"{0}\"", token);  
  24.   
  25.         HttpWebRequest request = HttpWebRequest.Create(userUrl) as HttpWebRequest;  
  26.         request.ContentLength = 0;  
  27.         request.Method = "GET";  
  28.         request.Headers["Authorization"] = headerValue;  
  29.   
  30.         HttpWebResponse response = request.GetResponse() as HttpWebResponse;  
  31.         using (StreamReader reader = new StreamReader(response.GetResponseStream()))  
  32.         {  
  33.             string obj = reader.ReadToEnd();  
  34.             Console.Write(obj);  
  35.             Console.ReadLine();  
  36.         }  
  37.     }  
  38.   
  39.     /// <summary>   
  40.     /// Get Token from ACS.   
  41.     /// </summary>   
  42.     /// <param name="scope"></param>   
  43.     /// <returns></returns>   
  44.     private static string GetTokenFromACS(string scope)  
  45.     {  
  46.         string wrapPassword = pwd;  
  47.         string wrapUsername = uid;  
  48.   
  49.         // request a token from ACS   
  50.         WebClient client = new WebClient();  
  51.         client.BaseAddress = string.Format("https://{0}.{1}", serviceNamespace, acsHostUrl);  
  52.   
  53.         NameValueCollection values = new NameValueCollection();  
  54.         values.Add("wrap_name", wrapUsername);  
  55.         values.Add("wrap_password", wrapPassword);  
  56.         values.Add("wrap_scope", scope);  
  57.   
  58.         byte[] responseBytes = client.UploadValues("WRAPv0.9/", "POST", values);  
  59.   
  60.         string response = Encoding.UTF8.GetString(responseBytes);  
  61.   
  62.         Console.WriteLine("\nreceived token from ACS: {0}\n", response);  
  63.   
  64.         return HttpUtility.UrlDecode(  
  65.             response  
  66.             .Split('&')  
  67.             .Single(value => value.StartsWith("wrap_access_token=", StringComparison.OrdinalIgnoreCase))  
  68.             .Split('=')[1]);  
  69.     }  
  70.   
  71.   
  72. }  

好了,你也可以试试运行一下这个控制台程序。

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款