【问题标题】:AAD: How uniquely identify users in a Web API?AAD:如何在 Web API 中唯一标识用户?
【发布时间】:2017-07-26 20:32:26
【问题描述】:

ASP.NET Web API 中,我需要能够识别具有唯一ID 的用户,以便可以在另一个数据库中引用他们。所有用户都在Azure Active Directory。我不想要会随着时间而改变的东西 (like a SID)。

我尝试使用 Membership 获取用户 Guid,例如 this,但我发现 Azure Active Directory does not support Memberships

我发现IdentityExtensions.GetUserId() 是这样使用的:User.Identity.GetUserId()。生成的 id 似乎既不是 Guid 也不是 SID。但是,它似乎对每个用户都是独一无二的。

User.Identity.GetUserId() 可以用来唯一标识 AAD 用户吗?如果没有,正确的做法是什么?

【问题讨论】:

    标签: c# asp.net azure active-directory


    【解决方案1】:

    可以使用 User.Identity.GetUserId() 来唯一标识 AAD 用户吗?

    是的,如果它使用 ClaimTypes.NameIdentifier。

    你怎么知道(F12/ctrl+F12除外)?

    默认情况下,它应该使用 ClaimTypes.NameIdentifier,前提是您使用的是 ASP 标识或类似代码(旧成员资格,基本上是 Microsoft 实现)。

        <ClaimsIdentityOptions.cs>
    
        /// <summary>
        /// Gets or sets the ClaimType used for the user identifier claim.
        /// </summary>
        /// <remarks>
        /// This defaults to <see cref="ClaimTypes.NameIdentifier"/>.
        /// </remarks>
        public string UserIdClaimType { get; set; } = ClaimTypes.NameIdentifier;
    

    IdentityOptionsTest.cs 中有一个名为 VerifyDefaultOptions 的单元测试,可用于确认默认设置。 导航到实现确实是唯一可以确定的方法。但是,如果您没有更改默认值,则不需要它。

    Original explanation regarding NameIdentifier here

    ASP Identity source code

    【讨论】:

    • 我怎么知道它是否在使用ClaimTypes.NameIdentifier
    • 更新了答案。这有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多