asp.net – Web API将OAuth令牌作为XML返回

前端之家收集整理的这篇文章主要介绍了asp.net – Web API将OAuth令牌作为XML返回前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用具有单个用户帐户的默认Visual Stu@R_301_410@ 2013 Web API项目模板,并使用application / xml的Accept标头发布到/ token端点,服务器仍然以 JSON方式返回响应:
  1. {"access_token":"...","token_type":"bearer","expires_in":1209599}

有没有办法将令牌恢复为XML?

解决方法

根据 RFC6749,响应格式应该是JSON,Microsoft会相应地实现它.我发现JSON格式是在Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerHandler内部类中实现的,没有任何扩展方式.

我还遇到了在XML中进行令牌响应的需要.
我提出的最佳解决方案是在Accept头中声明时实现将JSON转换为XML的HttpModule.

  1. public class OAuthTokenXmlResponseHttpModule : IHttpModule
  2. {
  3. private static readonly string FilterKey = typeof(OAuthTokenXmlResponseHttpModule).Name + typeof(MemoryStreamFilter).Name;
  4.  
  5. public void Init(HttpApplication application)
  6. {
  7. application.BeginRequest += ApplicationOnBeginRequest;
  8. application.EndRequest += ApplicationOnEndRequest;
  9. }
  10.  
  11. private static void ApplicationOnBeginRequest(object sender,EventArgs eventArgs)
  12. {
  13. var application = (HttpApplication)sender;
  14.  
  15. if (ShouldConvertToXml(application.Context.Request) == false) return;
  16.  
  17. var filter = new MemoryStreamFilter(application.Response.Filter);
  18. application.Response.Filter = filter;
  19. application.Context.Items[FilterKey] = filter;
  20. }
  21.  
  22. private static bool ShouldConvertToXml(HttpRequest request)
  23. {
  24. var isTokenPath = string.Equals("/token",request.Path,StringComparison.InvariantCultureIgnoreCase);
  25. var header = request.Headers["Accept"];
  26.  
  27. return isTokenPath && (header == "text/xml" || header == "application/xml");
  28. }
  29.  
  30. private static void ApplicationOnEndRequest(object sender,EventArgs eventArgs)
  31. {
  32. var context = ((HttpApplication) sender).Context;
  33.  
  34. var filter = context.Items[FilterKey] as MemoryStreamFilter;
  35. if (filter == null) return;
  36.  
  37. var jsonResponse = filter.ToString();
  38. var xDocument = JsonConvert.DeserializeXNode(jsonResponse,"oauth");
  39. var xmlResponse = xDocument.ToString(SaveOptions.DisableFormatting);
  40.  
  41. WriteResponse(context.Response,xmlResponse);
  42. }
  43.  
  44. private static void WriteResponse(HttpResponse response,string xmlResponse)
  45. {
  46. response.Clear();
  47. response.ContentType = "application/xml;charset=UTF-8";
  48. response.Write(xmlResponse);
  49. }
  50.  
  51. public void Dispose()
  52. {
  53. }
  54. }
  55.  
  56. public class MemoryStreamFilter : Stream
  57. {
  58. private readonly Stream _stream;
  59. private readonly MemoryStream _memoryStream = new MemoryStream();
  60.  
  61. public MemoryStreamFilter(Stream stream)
  62. {
  63. _stream = stream;
  64. }
  65.  
  66. public override void Flush()
  67. {
  68. _stream.Flush();
  69. }
  70.  
  71. public override int Read(byte[] buffer,int offset,int count)
  72. {
  73. return _stream.Read(buffer,offset,count);
  74. }
  75.  
  76. public override void Write(byte[] buffer,int count)
  77. {
  78. _memoryStream.Write(buffer,count);
  79. _stream.Write(buffer,count);
  80. }
  81.  
  82. public override string ToString()
  83. {
  84. return Encoding.UTF8.GetString(_memoryStream.ToArray());
  85. }
  86.  
  87. #region Rest of the overrides
  88. public override bool CanRead
  89. {
  90. get { throw new NotImplementedException(); }
  91. }
  92.  
  93. public override bool CanSeek
  94. {
  95. get { throw new NotImplementedException(); }
  96. }
  97.  
  98. public override bool CanWrite
  99. {
  100. get { throw new NotImplementedException(); }
  101. }
  102.  
  103. public override long Seek(long offset,SeekOrigin origin)
  104. {
  105. throw new NotImplementedException();
  106. }
  107.  
  108. public override void SetLength(long value)
  109. {
  110. throw new NotImplementedException();
  111. }
  112.  
  113. public override long Length
  114. {
  115. get { throw new NotImplementedException(); }
  116. }
  117.  
  118. public override long Position
  119. {
  120. get
  121. {
  122. throw new NotImplementedException();
  123. }
  124. set
  125. {
  126. throw new NotImplementedException();
  127. }
  128. }
  129. #endregion
  130. }

猜你在找的asp.Net相关文章