0

NTLM 認証RestKitでサポートされていますか? プロジェクトでどのように使用できますか?

4

2 に答える 2

0

RKObjectManager をサブクラス化してオーバーライドする必要がありましたgetObjectsAtPath:

- (void)getObjectsAtPath:(NSString *)path
          parameters:(NSDictionary *)parameters
             success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
             failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
    NSParameterAssert(path);
    RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodGET path:path parameters:parameters];
    [operation setCompletionBlockWithSuccess:success failure:failure];

    //this is the part to handle ntlm authentication, which we arent able to do in RKObjectManager
    [[operation HTTPRequestOperation] setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
        NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    }];

    [self enqueueObjectRequestOperation:operation];
}
于 2013-04-18T19:16:31.183 に答える
0

現在、RestKit は NTML 認証をサポートしていませんが、回避策があります。 1. ログイン ページで、NSURLRequest を使用して、認証を要求する URL をロードし、認証委任を登録します。

    - (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodNTLM)
    {
        /*    This is very, very important to check.  Depending on how your security policies are setup, you could lock your user out of his or her account by trying to use the wrong credentials too many times in a row.    */
        if ([challenge previousFailureCount] > 0)
        {
            [[challenge sender] cancelAuthenticationChallenge:challenge];

            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Invalid Credentials" message:@"The credentials you saved for your account are invalid." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
        else
        {
            [[challenge sender]  useCredential:[NSURLCredential credentialWithUser:@"someUser" password:@"somePassword" persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];
        }
    }
    else
    {
        //    Do whatever you want here, for educational purposes, I'm just going to cancel the challenge
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}

それ以外のページでは、認証なしで通常どおり RestKit を使用できます。認証をCookieに保持するため、SharePointサーバーでテストしただけですが、他のサーバーで動作するかどうかはわかりません。

楽しむ!:)

PS: iOS で NTLM 認証を行う方法

于 2012-07-16T04:44:50.370 に答える