加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 在OS X上读取登录用户的域

发布时间:2020-12-14 17:33:04 所属栏目:百科 来源:网络整理
导读:有没有办法确定登录帐户是OS X上的本地帐户还是活动目录帐户?如果是,我们如何检索域名? 解决方法 您可以从用户名为用户创建 CBUserIdentity : CBUserIdentity* identity = [CBUserIdentity identityWithName:NSUserName() authority:[CBIdentityAuthority
有没有办法确定登录帐户是OS X上的本地帐户还是活动目录帐户?如果是,我们如何检索域名?

解决方法

您可以从用户名为用户创建 CBUserIdentity

CBUserIdentity* identity = [CBUserIdentity identityWithName:NSUserName() authority:[CBIdentityAuthority defaultIdentityAuthority]];

然后,您可以获得该用户标识的authority:

CBIdentityAuthority* authority = identity.authority;

然后,您可以看到这是否是本地权限(替代方法是托管权限):

if ([authority isEqual:[CBIdentityAuthority localIdentityAuthority])
{
    // user is local
}
else
{
    // user is managed
}

权限有一个localizedName属性,但不太可能包含域名,我不这么认为.我不知道该怎么做.

更新:

这是一种使用Open Directory API的方法:

ODSession* session = [ODSession defaultSession];
ODNode* node = [ODNode nodeWithSession:session type:kODNodeTypeAuthentication error:NULL];
ODQuery* query = [ODQuery queryWithNode:node forRecordTypes:kODRecordTypeUsers attribute:kODAttributeTypeRecordName matchType:kODMatchEqualTo queryValues:NSUserName() returnAttributes:kODAttributeTypeStandardOnly maximumResults:0 error:NULL];
NSArray* results = [query resultsAllowingPartial:NO error:NULL];
ODRecord* record = results.firstObject;

此时,您可以查询记录的某些属性.可能感兴趣的可能是kODAttributeTypeMetaNodeLocation:

NSArray* attributes = [record valuesForAttribute:kODAttributeTypeMetaNodeLocation error:NULL];
NSString* attribute = attributes.firstObject;

对于本地帐户,元节点位置应为“/ Local / Default”.我使用LDAP帐户进行了测试,并提供了“”/LDAPv3/my.ldap.server.example.com“.我没有要测试的Active Directory帐户.

或者,您可以尝试kODAttributeTypeMetaRecordName.对于本地帐户,返回nil.对于LDAP帐户,它提供了完全可分辨的名称:“uid = ken,ou = People,dc = example,dc = com”.同样,我不知道它会对Active Directory帐户做什么.

您可以记录记录以查看可用的其他属性.这将把属性键显示为字符串值.您可以查看here以尝试为感兴趣的人找到符号常量,或者查看/System/Library/Frameworks/OpenDirectory.framework/Frameworks/CFOpenDirectory.framework/Headers/CFOpenDirectoryConstants.h以查找某些不感兴趣的常量.记录.

一旦找到了您真正关心的内容,您可以通过仅请求那些而不是kODAttributeTypeStandardOnly来简化查询.此外,您应该考虑异步运行查询,而不是像我在示例代码中那样同步运行查询.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读