【问题标题】:Retrieving stored passwords from keychain fails outside XCode在 XCode 之外从钥匙串中检索存储的密码失败
【发布时间】:2012-08-05 08:34:57
【问题描述】:

我按照“钥匙串服务编程指南”中 Apple 的示例代码将通用密码存储在钥匙串中。

只要我从 Xcode 以调试模式运行应用程序,一切正常。但是,当我存档和导出应用程序时,它仍会存储密码(在钥匙串访问中可见)但无法检索它们。

钥匙串不断返回 errSecAuthFailed (-25293)。这发生在山狮身上,但不会发生在雪豹身上。我的应用程序经过代码签名和沙盒处理。在我看来,在检索密码时,钥匙串不会将应用程序识别为存储密码的同一个应用程序,因为当我将密码设置为任何应用程序都可以访问时,它也能正常工作。

我使用以下代码:

+ (NSString*) retrievePasswordFromKeychainWithKey: (NSString*) theKey {    
    SecKeychainUnlock(NULL, 0, NULL, FALSE);
    const char* userNameUTF8 = [NSUserName() UTF8String];
    uint32_t userNameLength = (uint32_t)strlen(userNameUTF8);
    uint32_t serviceNameLength = (uint32_t)strlen([theKey UTF8String]);

    uint32_t pwLength = 0; 
    void* pwBuffer = nil; 
    SecKeychainItemRef itemRef = nil;
    OSStatus status1 = SecKeychainFindGenericPassword (NULL, serviceNameLength,  serviceNameUTF8, userNameLength, userNameUTF8, &pwLength, &pwBuffer, &itemRef);

    if (status1 == noErr) {
        NSData* pwData = [NSData dataWithBytes:pwBuffer length:pwLength];
        SecKeychainItemFreeContent (NULL,     //No attribute data to release
                                    pwBuffer    //Release data buffer allocated by SecKeychainFindGenericPassword
                                    );
        return [NSString stringWithCString:[pwData bytes] encoding:NSUTF8StringEncoding];
    }
    //status1 is always -25293
    return nil;   
}

【问题讨论】:

    标签: objective-c macos osx-mountain-lion


    【解决方案1】:

    好的,我刚刚得知这是 Mac OS 10.8.0 中的一个开放错误。使用 Developer ID 签名的应用无法访问钥匙串中的数据。 我希望这将在 10.8.1 中修复...

    解决方法是不要使用您的开发者 ID 对应用程序进行签名。 (我也读过在 Lion 下构建的应用程序不受此错误的影响,但我无法对此进行测试)

    【讨论】:

    • 我在 10.8 上遇到了同样的问题,非常有趣。我计划在 MacApp 商店中销售我的应用程序,这是我要解决的问题之一。
    • 此问题已在 10.8.1 中静默修复。
    猜你喜欢
    • 2018-03-30
    • 2017-09-05
    • 1970-01-01
    • 2021-10-01
    • 2023-03-29
    • 2017-06-03
    • 1970-01-01
    • 2017-03-31
    • 2013-05-31
    相关资源
    最近更新 更多