1

我有一种感觉,我错过了一些非常明显的东西......

我将 ADAL 与本机 iOS 应用程序一起使用。连接到 Azure 等

直到最近,这个流程对我有用。到今天为止,它似乎已经坏掉了。

我有一个从 iOS 的 office365 sdk 获得的 LogInController。以下方法:

-(void) getTokenWith :(NSString *)resourceId : (BOOL) clearCache completionHandler:(void (^) (NSString *))completionBlock; {
if([self getCacheToken : resourceId completionHandler:completionBlock]) return;

ADAuthenticationError *error;
authContext = [ADAuthenticationContext authenticationContextWithAuthority:authority error:&error];

NSURL *redirectUri = [NSURL URLWithString:redirectUriString];

[authContext acquireTokenWithResource:resourceId
                             clientId:clientId
                          redirectUri:redirectUri
                      completionBlock:^(ADAuthenticationResult  *result) {

                          if (AD_SUCCEEDED != result.status){
                              [self showError:result.error.errorDetails];
                          }
                          else{
                              NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                              [userDefaults setObject:result.tokenCacheStoreItem.userInformation.userId forKey:@"LogInUser"];
                              [userDefaults synchronize];

                              completionBlock(result.accessToken);
                          }
                      }];}

我调用的这个方法在我的 rootViewController 上的 viewDidLoad() 中被调用。

- (void)logIn {
_resourceId = [[NSUserDefaults standardUserDefaults] objectForKey:@"SharePointResourceId"];
_userURL = [NSString stringWithFormat:@"%@%@", _resourceId, SharePointResourceUserEndPoint];

LogInController *logInController = [[LogInController alloc] init];

[logInController getTokenWith:_resourceId :false completionHandler:^(NSString *token) {

    if (token == nil) {

        NSLog(@"newADALLogIn: No user access token exists");

    } else {
        //User is authenicated, Go get the Current user data object
        NSString *userToken = token;
        NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LogInUser"];
        [userId stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *completeUrl = [NSString stringWithFormat:@"%@%@%@", _userURL, userId, UserSelectProperties];
        NSString *authTokenBearer = [NSString stringWithFormat:@"Bearer %@", userToken];

        NSURL *URL = [NSURL URLWithString:completeUrl];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
        [request setValue:authTokenBearer forHTTPHeaderField:@"Authorization"];
        [request setValue:@"application/json" forHTTPHeaderField:@"accept"];

        @try {
            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
            operation.responseSerializer = [AFJSONResponseSerializer serializer];

            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

                //Doing UI stuff

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Error: %@", error);
            }];

            [[NSOperationQueue mainQueue] addOperation:operation];
        }
        @catch (NSException *exception) {
            NSLog(@"ERROR: %@", exception);
        }
    }
}];}

我现在遇到的问题是用户第一次登录。它正确地重定向到 o365 web auth 视图。用户能够很好地进行身份验证,然后显示主视图控制器。但是,代码:

//User is authenicated, Go get the Current user data object
        NSString *userToken = token;
        NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LogInUser"];
        [userId stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *completeUrl = [NSString stringWithFormat:@"%@%@%@", _userURL, userId, UserSelectProperties];
        NSString *authTokenBearer = [NSString stringWithFormat:@"Bearer %@", userToken];

        NSURL *URL = [NSURL URLWithString:completeUrl];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
        [request setValue:authTokenBearer forHTTPHeaderField:@"Authorization"];
        [request setValue:@"application/json" forHTTPHeaderField:@"accept"];

        @try {
            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
            operation.responseSerializer = [AFJSONResponseSerializer serializer];

            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

                //Doing UI stuff

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Error: %@", error);
            }];

            [[NSOperationQueue mainQueue] addOperation:operation];
        }
        @catch (NSException *exception) {
            NSLog(@"ERROR: %@", exception);
        }

永远不会被调用。过去,在 auth 之后,该代码将执行。任何建议都值得赞赏。

4

0 回答 0