【问题标题】:append additional information in a web request for authentication在 Web 请求中附加附加信息以进行身份​​验证
【发布时间】:2014-07-23 23:20:51
【问题描述】:

我正在为 OAuth 2.0 实现自己的身份验证框架。 据我了解,如果令牌已过期,服务器会发送 401。

我实现了 NSURLConnection 的委托

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

捕捉这些错误并刷新令牌。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
// refresh token and send it to server

    if ([challenge previousFailureCount] > 0) {
            // do something may be alert message
        } 
    else
    {
         //refreshToken
    }
}

但似乎我无法将令牌附加到 url。

【问题讨论】:

    标签: ios oauth nsurlconnection nsurl


    【解决方案1】:

    你不能 - 更改 url 意味着一个新的请求。当前请求正在进行中,并且是针对此 URL 的。

    一种直接的方法是在 url 连接本身周围有一个“包装器对象”,如果第一次失败,它可以透明地执行第二次请求

    例如伪代码

    @interface MyConnection
    - loadRequest:(NSURLRequest*)r completionHandler:handler //not fully felshed out for this example
    @end
    
    @implementation MyConnection
    
    - loadRequest:(NSURLRequest*)r completionHandler:handler //not fully felshed out for this example 
    {
        [NSURLConnection sendRequest:r completion:{
            if(response.status == 401)
            {
                NSMutableURLRequest *r2 = [NSMutableURLRequest requestWithURL:r.URL + token];
                //refresh by sending a new request r2
                    [NSURLConnection sendRequest:r2 completion:{
                        handler(response);
                    }];
            }
            else {
                handler(response);
            }
        }];
    }
    @end
    

    【讨论】:

      猜你喜欢
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多