- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此 OS X 钥匙串(keychain)包含三部分信息:
我显然总是知道 ServiceName。有没有办法找到该 ServiceName 的任何已保存的用户名? (一旦您知道用户名,找到密码就很容易了。)
我更喜欢使用漂亮的 cocoa wrapper ,例如 EMKeychain去做这个。但 EMKeychain 需要用户名才能获取任何钥匙串(keychain)项目!
+ (EMGenericKeychainItem *)genericKeychainItemForService:(NSString *)serviceNameString withUsername:(NSString *)usernameString;
如果您需要用户名来查找凭据,您如何充分利用钥匙串(keychain)中保存的凭据?最佳实践是将用户名保存在 .plist 文件或其他文件中吗?
最佳答案
SecKeychainFindGenericPassword
仅返回单个钥匙串(keychain)项。要查找特定服务的所有通用密码,您需要在钥匙串(keychain)上运行查询。根据您的目标 OS X 版本,有多种方法可以实现此目的。
如果您需要在 10.5 或更低版本上运行,则需要使用 SecKeychainSearchCreateFromAttributes
。这是一个相当可怕的 API。以下是返回将用户名映射到密码的字典的方法的粗略版本。
- (NSDictionary *)genericPasswordsWithService:(NSString *)service {
OSStatus status;
// Construct a query.
const char *utf8Service = [service UTF8String];
SecKeychainAttribute attr = { .tag = kSecServiceItemAttr,
.length = strlen(utf8Service),
.data = (void *)utf8Service };
SecKeychainAttribute attrList = { .count = 1, .attr = &attr };
SecKeychainSearchRef *search = NULL;
status = SecKeychainSearchCreateFromAttributes(NULL, kSecGenericPasswordItemClass, &attrList, &search);
if (status) {
report(status);
return nil;
}
// Enumerate results.
NSMutableDictionary *result = [NSMutableDictionary dictionary];
while (1) {
SecKeychainItemRef item = NULL;
status = SecKeychainSearchCopyNext(search, &item);
if (status)
break;
// Find 'account' attribute and password value.
UInt32 tag = kSecAccountItemAttr;
UInt32 format = CSSM_DB_ATTRIBUTE_FORMAT_STRING;
SecKeychainAttributeInfo info = { .count = 1, .tag = &tag, .format = &format };
SecKeychainAttributeList *attrList = NULL;
UInt32 length = 0;
void *data = NULL;
status = SecKeychainItemCopyAttributesAndData(item, &info, NULL, &attrList, &length, &data);
if (status) {
CFRelease(item);
continue;
}
NSAssert(attrList->count == 1 && attrList->attr[0].tag == kSecAccountItemAttr, @"SecKeychainItemCopyAttributesAndData is messing with us");
NSString *account = [[[NSString alloc] initWithBytes:attrList->attr[0].data length:attrList->attr[0].length encoding:NSUTF8StringEncoding] autorelease];
NSString *password = [[[NSString alloc] initWithBytes:data length:length encoding:NSUTF8StringEncoding] autorelease];
[result setObject:password forKey:account];
SecKeychainItemFreeAttributesAndData(attrList, data);
CFRelease(item);
}
CFRelease(search);
return result;
}
对于 10.6 及更高版本,您可以使用不太方便的 SecItemCopyMatching
API:
- (NSDictionary *)genericPasswordsWithService:(NSString *)service {
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
kSecClassGenericPassword, kSecClass,
(id)kCFBooleanTrue, kSecReturnData,
(id)kCFBooleanTrue, kSecReturnAttributes,
kSecMatchLimitAll, kSecMatchLimit,
service, kSecAttrService,
nil];
NSArray *itemDicts = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)q, (CFTypeRef *)&itemDicts);
if (status) {
report(status);
return nil;
}
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (NSDictionary *itemDict in itemDicts) {
NSData *data = [itemDict objectForKey:kSecValueData];
NSString *password = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSString *account = [itemDict objectForKey:kSecAttrAccount];
[result setObject:password forKey:account];
}
[itemDicts release];
return result;
}
对于10.7或更高版本,您可以使用我的精彩LKKeychain框架(插头!)。它不支持构建基于属性的查询,但您可以简单地列出所有密码并过滤掉不需要的密码。
- (NSDictionary *)genericPasswordsWithService:(NSString *)service {
LKKCKeychain *keychain = [LKKCKeychain defaultKeychain];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (LKKCGenericPassword *item in [keychain genericPasswords]) {
if ([service isEqualToString:item.service]) {
[result setObject:item.password forKey:item.account];
}
}
return result;
}
(我没有尝试运行,甚至没有编译任何上述代码示例;对于任何拼写错误,我们深表歉意。)
关于macos - 仅使用服务名称获取存储在钥匙串(keychain)中的用户名?或 : Where are you supposed to store the Username?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8451239/
这被要求作为这个 SO 的跟进: Add a keychain to search list? 我们知道如何通过以下方式将新创建的钥匙串(keychain)添加到搜索列表中: security lis
我一直在尝试在 iPhone 应用程序中使用持久钥匙串(keychain)引用。我发现如果我创建了两个不同的钥匙串(keychain)项目,我每次都会得到不同的持久引用(它们看起来像“genp....
无论我使用哪个访问组,我都无法在 iOS 模拟器上的 Keychain 中保存数据。使用真实设备一切正常。是否可以在模拟器上使用钥匙串(keychain)? 顺便说一句,我使用 SwiftKeycha
直到 Xcode 8/iOS 10,我才能够从静态库的单元测试中访问钥匙串(keychain)。升级后,出现错误,请参阅堆栈跟踪: failed: caught "NSInternalInconsis
我正在开发一个应用程序,我需要使用钥匙串(keychain)存储和检索用户数据,如(用户名、密码、url、域名等)。但我发现有可用的预定义键,例如(kSecAttrAccount、kSecAttrDe
有5 types of keys可以存储在 iOS 钥匙串(keychain)中 kSecClassGenericPassword kSecClassInternet 密码 kSecClassCert
我们有一个启动守护进程(出于各种原因,它必须以 root 身份运行),并通过网络与服务器组件进行通信。它需要通过服务进行身份验证,因此当它第一次获取密码时,我们将其保存到系统钥匙串(keychain)
我使用 iOS keychain wrapper 编写了一个项目存储用户名和密码,项目工作正常,直到昨天。 在我对项目运行 clean 命令后,项目崩溃: - (void)writeToKeychai
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
为了授予快速用户身份验证,想法是使用 wifi(企业)网络的相同凭据。是否可以从应用程序获取用于网络访问的用户/密码? 我的意思是,以编程方式访问设备钥匙串(keychain)? 最佳答案 从此app
我实现了这段代码 https://github.com/exchangegroup/keychain-swift使用钥匙串(keychain)。我作为一个该死的初学者的问题: 我使用 parse.co
这一切都发生在我的模拟器中。在练习使用钥匙串(keychain)包装时,我发现自己处于一种奇怪的情况。我试图学习如何从钥匙串(keychain)插入和检索数据,为了做到这一点,我开始了一个新项目,其中
我一直在阅读该文档,但我感到困惑。 有iOS钥匙串(keychain)here . 还有 iCloud 钥匙串(keychain) here . 它们都称为钥匙串(keychain),都能够本地存储并
我最近买了一台新的 Macbook Pro,并将我的旧数据迁移到了它上面。从那时起,Xcode 拒绝在物理设备上构建。模拟器工作正常(因为我认为它不需要代码签名)。 我收到的错误是这样的:“在钥匙串(
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在脚本中运行 xcodebuild 来存档和导出我的项目。我正在使用钥匙串(keychain)文件进行代码签名。为了避免提示输入钥匙串(keychain)密码,我使用 security unloc
我正在使用 KeychainItemWrapper 类(项目中集成的 .h 和 .m 文件)在 Keychain for iOS App 中保存密码。还在项目中需要的地方导入“安全”框架和“keych
我正在尝试使用 Jenkins 打包一个 iOS ipa。所以我得到了 Keychains and Provisioning Profiles Management 插件并尝试上传它需要的钥匙串(ke
我在调用 SecItemAdd 时没有找到有关此类错误代码的信息,可能是什么原因造成的? 提前致谢 编辑:这是我得到错误的函数: + (BOOL)storeWithKey:(NSString *)ke
我正在尝试签署 iPhone 应用程序以供发布,因此我按照说明进行操作。无论如何,在创建我的证书签名请求时,我从钥匙串(keychain)访问中收到此错误:“找不到默认钥匙串(keychain)” 我
我是一名优秀的程序员,十分优秀!