<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      美國銀行卡Payoneer注冊獎勵25美元

      image

      各種語言實現的oauth認證: http://oauth.net/code/

       

      上一篇文章介紹了如何使用基本的http認證來實現asp.net web api的跨平臺安全認證。 這里說明一個如何使用oauth實現的認證。oauth大家可能不陌生。那么這里需要注意的是我們使用的是.net平臺一個比較好的開源oauth庫。 DOTNETOPENAUTH。

      就像上圖所示,我們需要一個ISSSUE Server來給我們一個token,然后再去資源服務器請求資源,也就是Web API Server。

      image

       

      首先在oAuthIssuer服務端我們需要實現一個DotNetOpenAuth的接口:IAuthorizationServer

      image

       

      對接口的實現:

       

      public class OAuth2Issuer : IAuthorizationServer
          {
              private readonly IssuerConfiguration _configuration;
      
              public OAuth2Issuer(IssuerConfiguration configuration)
              {
                  if (configuration == null) throw new ArgumentNullException("configuration");
                  _configuration = configuration;
              }
      
              public RSACryptoServiceProvider AccessTokenSigningKey
              {
                  get
                  {
                      return (RSACryptoServiceProvider)_configuration.SigningCertificate.PrivateKey;
                  }
              }
      
              public DotNetOpenAuth.Messaging.Bindings.ICryptoKeyStore CryptoKeyStore
              {
                  get { throw new NotImplementedException(); }
              }
      
              public TimeSpan GetAccessTokenLifetime(DotNetOpenAuth.OAuth2.Messages.IAccessTokenRequest accessTokenRequestMessage)
              {
                  return _configuration.TokenLifetime;
              }
      
              public IClientDescription GetClient(string clientIdentifier)
              {
                  const string secretPassword = "test1243";
                  return new ClientDescription(secretPassword, new Uri("http://localhost/"), ClientType.Confidential);
              }
      
              public RSACryptoServiceProvider GetResourceServerEncryptionKey(DotNetOpenAuth.OAuth2.Messages.IAccessTokenRequest accessTokenRequestMessage)
              {
                  return (RSACryptoServiceProvider)_configuration.EncryptionCertificate.PublicKey.Key;
      
              }
      
              public bool IsAuthorizationValid(DotNetOpenAuth.OAuth2.ChannelElements.IAuthorizationDescription authorization)
              {
                  
                  //claims added to the token
                  authorization.Scope.Add("adminstrator");
                  authorization.Scope.Add("poweruser");
                  
                  return true;
              }
      
              public bool IsResourceOwnerCredentialValid(string userName, string password)
              {
                  return true;
              }
      
              public DotNetOpenAuth.Messaging.Bindings.INonceStore VerificationCodeNonceStore
              {
                  get
                  {
                      throw new NotImplementedException();
                  }
              }
          }

       

       

       

       

      在 Web API Server端,我們需要使用Http Message Handler來獲取httprequest信息;并進行是否有授權認證。

       

       public class OAuth2Handler : DelegatingHandler
          {
              private readonly ResourceServerConfiguration _configuration;
      
              public OAuth2Handler(ResourceServerConfiguration configuration)
              {
                  if (configuration == null) throw new ArgumentNullException("configuration");
                  _configuration = configuration;
              }
      
              protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
              {
                  HttpContextBase httpContext;
                  string userName;
                  HashSet<string> scope;
      
                  if (!request.TryGetHttpContext(out httpContext))
                      throw new InvalidOperationException("HttpContext must not be null.");
      
                  var resourceServer = new ResourceServer(new StandardAccessTokenAnalyzer(
                                                              (RSACryptoServiceProvider)_configuration.IssuerSigningCertificate.PublicKey.Key,
                                                              (RSACryptoServiceProvider)_configuration.EncryptionVerificationCertificate.PrivateKey));
      
                  var error = resourceServer.VerifyAccess(httpContext.Request, out userName, out scope);
      
                  if (error != null)
                      return Task<HttpResponseMessage>.Factory.StartNew(error.ToHttpResponseMessage);
                  
                  var identity = new ClaimsIdentity(scope.Select(s => new Claim(s, s)));
                  if (!string.IsNullOrEmpty(userName))
                      identity.Claims.Add(new Claim(ClaimTypes.Name, userName));
                  
                  httpContext.User = ClaimsPrincipal.CreateFromIdentity(identity);
                  Thread.CurrentPrincipal = httpContext.User;
      
                  return base.SendAsync(request, cancellationToken);
              }
      
          }

       

       

       

       

      這里的ResourceServerConfiguration 我們是使用加密證書的。

      image

       

      客戶端調用代碼:

      image

       

      調用API獲取數據之前需要從IssueServer獲取Token。

      GetAccessToken:

       

       

      image

       

      看一下Token信息:

      {"access_token":"gAAAAIoUBVBrZ5jAxe5XeTgnJ8mGwwKsCReknueg4gLGlDQ77lR1yPfxt0yNfWLCBT7hxnHjRjuEwDTJ3J1YAnqML4MIgQg8A2cz2bs0EnxvCMfKnayKEesRM-lxLTFbWMpSxe2Xvjm61IbaXjrMkYDRMnV4Do8-7132tiOLIv02WOGlJAEAAIAAAACJ8F3SsE6cTI1XsioW_xOxHeESDzG16y01Gxm3HikYFUC3XIdekpPw0yMB4tavPmUj-kRyC1halbUX7JKf-Dihm6Ou5mexe9lcYTr9or_kH7WcDN5ZCryUK3OaecvwwjQVr5o9XD2ZyZSNDCNhVRFc5ypvP85zZCBW1KJkP3OTCV4AkMN-ROvgI8jxutYdsLLN-YbB7Ot5iypzWWbW0QxiwOzMEqG9nVtPwnIWOUMOvW5KbiELELhgjap60mwHzGrHG4TtA4jrNy8S9zjixO_q-FrgpAuC06CkSH-R4w9yPCLLDc9m3UoAnknFjd4PUbWLxCvlBpEK2sg03ENa0EOKzc2O5fEic9P-BiYt6afMwTgLkJlGBBjmCBpGZMkfLTw","token_type":"bearer","expires_in":"300","scope":"http:\/\/localhost\/ adminstrator poweruser"}

      image

       

      客戶端調用:

       

      image

       

       代碼:

       

       

      http://pan.baidu.com/s/1ntkMbCt

       

      posted on 2012-05-29 21:29  Nic Pei  閱讀(37678)  評論(55)    收藏  舉報
      主站蜘蛛池模板: 日韩亚av无码一区二区三区 | 粉嫩一区二区三区国产精品| 国产粉嫩区一区二区三区| 国产精品一码在线播放| 精品久久久久久无码不卡| 久热这里只有精品12| 国产视频不卡一区二区三区| 97色伦97色伦国产| 成在人线av无码免费| 2020年最新国产精品正在播放 | 色噜噜在线视频免费观看| 亚欧美闷骚院| 亚洲乱码一二三四区国产| 熟女乱一区二区三区四区| 91精品91久久久久久| 九九热精品免费视频| 国产一级区二级区三级区| 中文www天堂| 国产精品 亚洲一区二区三区 | 性欧美三级在线观看| 亚洲区色欧美另类图片| 国产色无码精品视频免费| 性视频一区| 一个人在看www免费| 亚洲色大成网站WWW久久| 最新精品国偷自产在线美女足| 亚洲中文日韩一区二区三区| 亚洲精品中文av在线| 国产无遮挡真人免费视频| 欧美成本人视频免费播放| 国产精品午夜福利小视频| 国产成AV人片久青草影院| 色欲国产精品一区成人精品| 无码av最新无码av专区| 色综合AV综合无码综合网站| 国内精品视频区在线2021| 精品国产迷系列在线观看| 孝感市| 亚洲熟女乱一区二区三区| 九九久久精品国产免费看小说| 人妻系列无码专区69影院|