如何扩展User.Identity的可用属性

问题描述 投票:115回答:6

我正在使用MVC5 Identity 2.0登录我的网站,其中身份验证详细信息存储在SQL数据库中。 Asp.net Identity已经以标准方式实现,可以在许多在线教程中找到。

IdentityModels中的ApplicationUser类已扩展为包含一些自定义属性,例如整数OrganizationId。我们的想法是,可以创建许多用户并将其分配给公共组织以实现数据库关系。

public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        //Extended Properties
        public DateTime? BirthDate { get; set; }
        public long? OrganizationId { get; set; }

        //Key Mappings
        [ForeignKey("OrganizationId")]
        public virtual Organization Organization { get; set; }
    }

如何从控制器中检索当前登录用户的OrganizationId属性?一旦用户登录,这是否可通过方法获得,或者每次执行控制器方法时,我是否始终根据UserId从数据库中检索OrganizationId?

在网上阅读我看到我需要使用以下内容来登录UserId等。

using Microsoft.AspNet.Identity;
...
User.Identity.GetUserId();

但是,OrganizationId不是User.Identity中可用的属性。我是否需要扩展User.Identity以包含OrganizationId属性?如果是这样,我该怎么做呢。

我经常需要OrganizationId的原因是许多表查询依赖于OrganizationId来检索与登录用户相关联的与组织相关的数据。

asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2
6个回答
200
投票

每当您想要使用上述问题等任何其他属性扩展User.Identity的属性时,首先将这些属性添加到ApplicationUser类,如下所示:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    // Your Extended Properties
    public long? OrganizationId { get; set; }
}

那么你需要的是创建一个像这样的扩展方法(我在一个新的Extensions文件夹中创建我的):

namespace App.Extensions
{
    public static class IdentityExtensions
    {
        public static string GetOrganizationId(this IIdentity identity)
        {
            var claim = ((ClaimsIdentity)identity).FindFirst("OrganizationId");
            // Test for null to avoid issues during local testing
            return (claim != null) ? claim.Value : string.Empty;
        }
    }
}

在ApplicationUser类中创建Identity时,只需添加Claim - > OrganizationId,如下所示:

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here => this.OrganizationId is a value stored in database against the user
        userIdentity.AddClaim(new Claim("OrganizationId", this.OrganizationId.ToString()));

        return userIdentity;
    }

添加声明并使用扩展方法后,要使其作为User.Identity的属性可用,请在要访问它的页面/文件上添加using语句:

在我的情况下:控制器中的qazxsw poi和.cshtml视图文件中的qazxsw poi。

编辑:

您还可以做的是避免在每个View中添加using语句,转到Views文件夹,然后在其中找到Web.config文件。现在查找using App.Extensions;标记并在其中添加扩展名称空间,如下所示:

@using. App.Extensions

保存文件,你就完成了。现在,每个View都会知道您的扩展程序。

您可以访问扩展方法:

<namespaces>

希望能帮到任何人:)


15
投票

我正在寻找相同的解决方案,Pawel给了我99%的答案。我需要扩展显示的唯一缺少的是将以下Razor代码添加到cshtml(视图)页面:

<add namespace="App.Extensions" />

我正在寻找名字,在用户登录后显示在我的NavBar的右上角。

我以为我会发布这个帮助其他人,所以这是我的代码:

我创建了一个名为Extensions的新文件夹(在我的模型文件夹下)并创建了上面指定的Pawel新类:var orgId = User.Identity.GetOrganizationId();

@using programname.Models.Extensions

IdentityExtensions.cs

using System.Security.Claims;
using System.Security.Principal;

namespace ProgramName.Models.Extensions
{
    public static class IdentityExtensions
    {
        public static string GetUserFirstname(this IIdentity identity)
        {
            var claim = ((ClaimsIdentity)identity).FindFirst("FirstName");
            // Test for null to avoid issues during local testing
            return (claim != null) ? claim.Value : string.Empty;
        }
    }
}

然后在我的IdentityModels.cs(在public class ApplicationUser : IdentityUser { //Extended Properties public string FirstName { get; internal set; } public string Surname { get; internal set; } public bool isAuthorized { get; set; } public bool isActive { get; set; } public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here userIdentity.AddClaim(new Claim("FirstName", this.FirstName)); return userIdentity; } } 文件夹下)我添加了_LoginPartial.cshtml

然后,我将更改添加到以下代码行,该代码行将在登录后使用“用户名”:

Views/Shared

也许这有助于其他人下线。


10
投票

看看John Atten撰写的这篇精彩博文:@using.ProgramName.Models.Extensions

它在整个过程中提供了很好的分步信息。去读它:)

以下是一些基础知识。

通过添加新属性(即地址,城市,州等)来扩展默认的ApplicationUser类:

@Html.ActionLink("Hello " + User.Identity.GetUserFirstname() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })

然后将新属性添加到R​​egisterViewModel。

ASP.NET Identity 2.0: Customizing Users and Roles

然后更新“注册视图”以包含新属性。

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> 
    GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this,  DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }

    // Use a sensible display name for views:
    [Display(Name = "Postal Code")]
    public string PostalCode { get; set; }

    // Concatenate the address info for display in tables and such:
    public string DisplayAddress
    {
        get
        {
            string dspAddress = string.IsNullOrWhiteSpace(this.Address) ? "" : this.Address;
            string dspCity = string.IsNullOrWhiteSpace(this.City) ? "" : this.City;
            string dspState = string.IsNullOrWhiteSpace(this.State) ? "" : this.State;
            string dspPostalCode = string.IsNullOrWhiteSpace(this.PostalCode) ? "" : this.PostalCode;

            return string.Format("{0} {1} {2} {3}", dspAddress, dspCity, dspState, dspPostalCode);
        }
    }

然后使用新属性更新AccountController上的Register()方法。

    // Add the new address properties:
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }

2
投票

对于任何发现此问题的人来说,如何在ASP.NET Core 2.1中访问自定义属性 - 它会更容易:您将拥有一个UserManager,例如在_LoginPartial.cshtml中,然后你可以简单地做(假设“ScreenName”是你添加到自己的AppUser的属性,它继承自IdentityUser):

    <div class="form-group">
        @Html.LabelFor(m => m.Address, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Address, new { @class = "form-control" })
        </div>
    </div>

1
投票

Dhaust提供了一种将属性添加到ApplicationUser类的好方法。看看OP代码,它们可能已经完成了这项工作,或者正在按计划进行。问题是问题

如何从控制器中检索当前登录用户的OrganizationId属性?但是,OrganizationId不是User.Identity中可用的属性。我是否需要扩展User.Identity以包含OrganizationId属性?

Pawel提供了一种添加扩展方法的方法,该方法需要使用语句或将命名空间添加到web.config文件中。

但是,问题是您是否“需要”扩展User.Identity以包含新属性。在不扩展User.Identity的情况下,可以使用另一种方法访问该属性。如果您遵循Dhaust方法,则可以在控制器中使用以下代码来访问新属性。

    // Add the Address properties:
    user.Address = model.Address;
    user.City = model.City;
    user.State = model.State;
    user.PostalCode = model.PostalCode;

0
投票

我还在AspNetUsers表中添加或扩展了其他列。当我想简单地查看这些数据时,我发现了许多例子,比如上面的代码和“Extensions”等......这让我很惊讶你必须编写所有这些代码行才能从当前用户那里获得一些值。

事实证明,您可以像任何其他表一样查询AspNetUsers表:

@using Microsoft.AspNetCore.Identity

@using <namespaceWhereYouHaveYourAppUser>

@inject SignInManager<AppUser> SignInManager
@inject UserManager<AppUser> UserManager

@if (SignInManager.IsSignedIn(User)) {
    <form asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })" 
          method="post" id="logoutForm" 
          class="form-inline my-2 my-lg-0">

        <ul class="nav navbar-nav ml-auto">
            <li class="nav-item">
                <a class="nav-link" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">
                    Hello @((await UserManager.GetUserAsync(User)).ScreenName)!
                    <!-- Original code, shows Email-Address: @UserManager.GetUserName(User)! -->
                </a>
            </li>
            <li class="nav-item">
                <button type="submit" class="btn btn-link nav-item navbar-link nav-link">Logout</button>
            </li>
        </ul>

    </form>
} else {
    <ul class="navbar-nav ml-auto">
        <li class="nav-item"><a class="nav-link" asp-area="Identity" asp-page="/Account/Register">Register</a></li>
        <li class="nav-item"><a class="nav-link" asp-area="Identity" asp-page="/Account/Login">Login</a></li>
    </ul>
}
© www.soinside.com 2019 - 2024. All rights reserved.