gpt4 book ai didi

ios - 基本身份验证示例

转载 作者:行者123 更新时间:2023-11-29 03:43:10 25 4
gpt4 key购买 nike

我一直在尝试弄清楚如何在我的应用程序中创建登录页面。我有 URL、有效的用户名、密码。我被告知要使用基本身份验证来做到这一点。

到目前为止我实现了这段代码

- (IBAction)loginClicked:(id)sender {

NSURL *url = [NSURL URLWithString:@"http://site-url.com"];

NSString *loginString =(NSMutableString*)[NSString stringWithFormat:@"%@:%@",usernameTextField.text,passwordTextField.text];

NSData *encodedLoginData=[loginString dataUsingEncoding:NSASCIIStringEncoding];

NSString *authHeader=[NSString stringWithFormat:@"Basic %@", [encodedLoginData base64Encoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:3.0];

[request setValue:authHeader forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"POST"];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}

- (void) connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response {

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

// POW, here's the content of the webserver's response.
NSLog(@"Response -> %@",response);

int statusCode = [httpResponse statusCode];

NSLog(@"response code:%d",statusCode);
}

- (void)connection:(NSURLConnection *)connection willReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:usernameTextField.text
password:passwordTextField.text
persistence:NSURLCredentialPersistenceForSession];
NSLog(@"credential created");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
NSLog(@"responded to authentication challenge");
}
else {
NSLog(@"previous authentication failure");
}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSLog(@"Connection success.");

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failure.");

让它工作,但当我使用我的网站 URL 时,它只会给我成功消息和状态代码 200,并且不会调用委托(delegate)方法 - willSendRequestForAuthenticationChallenge:

当我将 URL 更改为 gmail.com 时,它会提供以下输出:

2013-08-06 12:06:08.674 CVReaderSGT[26544:a0b] received authentication challenge 2013-08-06 12:06:08.674 CVReaderSGT[26544:a0b] Username string:admin 2013-08-06 12:06:08.674 CVReaderSGT[26544:a0b] Password string:password 2013-08-06 12:06:08.675 CVReaderSGT[26544:a0b] credential created - > : admin 2013-08-06 12:06:08.675 CVReaderSGT[26544:a0b] responded to authentication challenge 2013-08-06 12:06:09.025 CVReaderSGT[26544:a0b] received authentication challenge 2013-08-06 12:06:09.026 CVReaderSGT[26544:a0b] Username string:admin 2013-08-06 12:06:09.026 CVReaderSGT[26544:a0b] Password string:password 2013-08-06 12:06:09.026 CVReaderSGT[26544:a0b] credential created - > : admin 2013-08-06 12:06:09.026 CVReaderSGT[26544:a0b] responded to authentication challenge 2013-08-06 12:06:10.069 CVReaderSGT[26544:a0b] received authentication challenge 2013-08-06 12:06:10.069 CVReaderSGT[26544:a0b] Username string:admin 2013-08-06 12:06:10.069 CVReaderSGT[26544:a0b] Password string:password 2013-08-06 12:06:10.069 CVReaderSGT[26544:a0b] credential created - > : admin 2013-08-06 12:06:10.070 CVReaderSGT[26544:a0b] responded to authentication challenge 2013-08-06 12:06:10.457 CVReaderSGT[26544:a0b] Response -> { URL: https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2 } { status code: 200, headers { "Cache-Control" = "no-cache, no-store"; "Content-Type" = "text/html; charset=UTF-8"; Date = "Tue, 06 Aug 2013 17:06:09 GMT"; Expires = "Mon, 01-Jan-1990 00:00:00 GMT"; Pragma = "no-cache"; Server = GSE; "Set-Cookie" = "GAPS=1:LoAnsovSVriBDm6iP1r7zuYy7bTCpQ:3o2OJzkd7X87BrCH;Path=/;Expires=Thu, 06-Aug-2015 17:06:09 GMT;Secure;HttpOnly, GALX=S0GMqJp_fyQ;Path=/;Secure"; "Strict-Transport-Security" = "max-age=2592000; includeSubDomains"; "Transfer-Encoding" = Identity; "X-Auto-Login" = "realm=com.google&args=service%3Dmail%26continue%3Dhttps%253A%252F%252Fmail.google.com%252Fmail%252F"; "X-Content-Type-Options" = nosniff; "X-Frame-Options" = Deny; "X-XSS-Protection" = "1; mode=block"; } } 2013-08-06 12:06:10.458 CVReaderSGT[26544:a0b] Status code -> 200 2013-08-06 12:06:11.243 CVReaderSGT[26544:a0b] Connection success.

我不知道为什么当我更改 URL 时会得到两个不同的输出,也不知道为什么当我输入 URL 时委托(delegate)方法 willSendRequestForAuthenticationChallenge: 没有被调用。

  Here's what the output looks like when using my URL:
2013-08-06 12:29:06.007 CVReaderSGT[26583:a0b] Response -> <NSHTTPURLResponse: 0x8e20ac0> { URL: http://XYZ.com/ } { status code: 403, headers {
"Content-Length" = 1585;
"Content-Type" = "text/html";
Date = "Tue, 06 Aug 2013 17:29:04 GMT";
MicrosoftSharePointTeamServices = "12.0.0.6219";
Server = "Microsoft-IIS/6.0";
"X-Powered-By" = "ASP.NET";
} }
2013-08-06 12:29:06.007 CVReaderSGT[26583:a0b] Status code -> 403
2013-08-06 12:29:06.007 CVReaderSGT[26583:a0b] Connection success.

我真的很想了解这个过程并让它发挥作用。

任何人都可以提供示例或提供正确的信息吗?谢谢。

最佳答案

// Authentication delegate methods
- (BOOL)connection:(NSURLConnection *)connection
canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
#pragma mark unused (connection)
assert(connection == connect);
assert(protectionSpace != nil);

BOOL retVal;
retVal = [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

return YES;
}

- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
// Tries to connect 3 times, after the 3 attempts it cancels.
if ([challenge previousFailureCount] < 3) {
// Create credentials from the user name and password and save them, for this
// specific authentication challenge.
NSURLCredential *credentials = [NSURLCredential credentialWithUser:user password:pass persistence:NSURLCredentialPersistenceForSession];
// [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// Handle the credentials according the state of the challenge.
if (challenge == nil) {
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
}
else {
[challenge.sender useCredential:credentials forAuthenticationChallenge:challenge];
}
}
else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog(@"Wrong username or password");
}
}

// Save credentials for further use.
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
return YES;
}

看看这个。这对我有用。尝试更改此处的值,看看哪个值符合您的服务器要求。

retVal = [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

关于ios - 基本身份验证示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18086408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com