代码之家  ›  专栏  ›  技术社区  ›  Adrian

从全名中获取短用户名

  •  2
  • Adrian  · 技术社区  · 15 年前

    有人知道如何得到一个用户的短用户名,如“john smith”,给出了他们的全名,如“john smith”?

    注意,我对任何用户都感兴趣,而不是当前用户,所以像nsusername这样的函数是不相关的。

    为什么?我正在使用授权服务验证用户名和密码。这允许人们输入他们的短名称或全名,这很好,但我需要知道他们实际上以谁的身份登录(即短用户名和/或用户ID)。

    像[nshomedirectoryforuser(username)lastpathcomponent]这样的恶意黑客不会始终如一地工作。

    1 回复  |  直到 15 年前
        1
  •  6
  •   Thibault Martin-Lagardette    15 年前

    你需要使用 Collaboration Framework :) 将此框架链接到项目,然后只需执行以下操作:

    CBIdentity* identity = [CBIdentity identityWithName:@"John Smith" authority:[CBIdentityAuthority localIdentityAuthority]];
    NSLog(@"Posix name: %@", [identity posixName]);
    

    还有Voice!

    编辑:如果只需要查找绑定到网络上的用户,则需要使用 +managedIdentityAuthority 而不是 +localIdentityAuthority . 如果需要同时查找本地用户和网络用户,请使用 +defaultIdentityAuthority .