CSRF Failed: CSRF token missing
2020-02-12 本文已影响0人
南城同學
1. 设置手动管理cookies生命周期 并主动获取;
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.requestSerializer.HTTPShouldHandleCookies = NO;
[manager.requestSerializer setValue:[UserManager iz_csrfToken] forHTTPHeaderField:@"Cookie"];
2. 在网络请求成功之后手动保存cookie
NSURLSessionDataTask *task = [manager POST:self.urlStr
parameters:self.dicParam
progress: nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSHTTPURLResponse* response = (NSHTTPURLResponse* )task.response;
NSDictionary *allHeaderFieldsDic = response.allHeaderFields;
NSString *setCookie = allHeaderFieldsDic[@"Set-Cookie"];
if (setCookie != nil) {
NSString *cookie = [[setCookie componentsSeparatedByString:@";"] objectAtIndex:0];
[UserManager iz_saveCsrfTken:cookie];
}
[self decodeMessage:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
[task resume];
}