Ios 如何使用NSMutableURLRequest正确包含.ASPXAUTH和ASP.NET_会话ID cookies

Ios 如何使用NSMutableURLRequest正确包含.ASPXAUTH和ASP.NET_会话ID cookies,ios,asp.net,cookies,nsmutableurlrequest,.aspxauth,Ios,Asp.net,Cookies,Nsmutableurlrequest,.aspxauth,我已经研究这个问题好几天了。我的应用程序需要登录到ASP.NET(版本:4.0.30319,MVC版本:3.0)服务器,然后通过NSMutableURLRequest发布到受限页面(需要您登录才能访问的页面) 目前,我可以成功登录到该网站。要登录,我使用以下代码: NSString *post =[[NSString alloc] initWithFormat:@"LogInEmail=%@&Password=%@",@"username@website.com",@"password"

我已经研究这个问题好几天了。我的应用程序需要登录到ASP.NET(版本:4.0.30319,MVC版本:3.0)服务器,然后通过NSMutableURLRequest发布到受限页面(需要您登录才能访问的页面)

目前,我可以成功登录到该网站。要登录,我使用以下代码:

NSString *post =[[NSString alloc] initWithFormat:@"LogInEmail=%@&Password=%@",@"username@website.com",@"password"];
NSURL *url=[NSURL URLWithString:@"http://www.website.com/LogIn"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseMeta = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseMeta error:&error];
我可以检查并看到该网站的ASP.NET_SessionId和.ASPXAUTH cookie都可以在SharedHttpCookie存储中找到。我明白,如果我想“登录”,这两个都需要随请求一起发送

检查cookies的代码如下:

NSArray *cookiesForURL = [cookieJar cookiesForURL: [NSURL URLWithString: @"http://www.example.com"]];

for (cookie in cookiesForURL)
{
    if([cookie.name compare:@"ASP.NET_SessionId"] == NSOrderedSame)
    {
        ASPdotNET_SessionId = [cookie value];
        NSLog(@"ASP.NET_SessionId: %@"  , ASPdotNET_SessionId);
    }
    if([cookie.name compare:@".ASPXAUTH"] == NSOrderedSame)
    {
        dotASPXAUTH = [cookie value];
        NSLog(@".ASPXAUTH: %@"          , dotASPXAUTH);
    }
}
要清除Cookie,请使用以下命令:

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *each in [[cookieStorage cookiesForURL:[NSURL URLWithString:@"http://www.example.com"]] copy]) {
    [cookieStorage deleteCookie:each];
}
我试图通过修改
NSString*post
NSUrl*url
并使用上面相同的代码来发布到受限页面

NSString *post =[[NSString alloc] initWithFormat:[MessageId stringByAppendingString:@"=%@"],Comment];
NSURL *url=[NSURL URLWithString:@"https://www.website.com/message
这是不成功的。我想是因为我在某种程度上忽略了饼干。我尝试在
[request setHTTPMethod:@“POST”]下面添加此代码

[request addValue:ASPdotNET_SessionId forHTTPHeaderField:@"Cookie"];
和/或:

[request addValue:dotASPXAUTH forHTTPHeaderField:@"Cookie"];
其中,
ASPdotNET\u SessionId
dotASPXAUTH
是从SharedHTTPCookie存储中检索到的cookie值


我觉得这真的很简单,有什么帮助吗?

经过更多的测试,当我认为身份验证失败时,似乎我错了。似乎是
NSURLConnection
NSMutableURLRequest
为我处理cookies,我不需要从
sharedHTTPCookieStorage
检索cookies,也不需要用我的请求设置cookies

此外,如果有人手动发送Cookie,他们应至少采用此格式,而不仅仅是纯值,因为ASP.NET希望Cookie采用特定格式:

[request setValue:[NSString stringWithFormat:@"ASP.NET_SessionId=%@", ASPdotNET_SessionId] forHTTPHeaderField:@"Cookie"];
[request setValue:[NSString stringWithFormat:@".ASPXAUTH=%@", dotASPXAUTH] forHTTPHeaderField:@"Cookie"];
或者,如果上述方法不起作用,则可能出现这种情况:

[request setValue:[[NSString stringWithFormat:@"ASP.NET_SessionId=%@", ASPdotNET_SessionId] stringByAppendingString:@"; path=/; HttpOnly"] forHTTPHeaderField:@"Cookie"];
[request setValue:[[NSString stringWithFormat:@".ASPXAUTH=%@", dotASPXAUTH] stringByAppendingString:@"; path=/; HttpOnly"] forHTTPHeaderField:@"Cookie"];
其中,
ASPdotNET\u SessionId
是24个字符的会话ID,
dotspxauth
是448个字符的身份验证字符串