原文链接:https://www.it566.top/index.php/wenzhang/else/2021/03/25/78398/
我使用mvc5identity2.0让用户登录到我的网站,身份验证细节存储在一个SQL数据库中。Asp.netIdentity已经以一种标准的方式实现,这可以在许多在线教程中找到。
IdentityModels中的ApplicationUser类已扩展为包含一些自定义属性,例如整数OrganizationId。其思想是为了数据库关系的目的,可以创建许多用户并将其分配给一个公共组织。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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?
在网上阅读,我看到我需要使用以下获得登录的用户名等。
1 2 3 |
using Microsoft.AspNet.Identity; ... User.Identity.GetUserId(); |
但是,OrganizationId不是User.Identity中的属性. 需要将User.Identity扩展为包含OrganizationId属性吗?如果是,该怎么办。
经常需要OrganizationId的原因是,许多表查询都依赖于OrganizationId来检索与登录用户关联的组织相关的数据。
对于任何类似上述问题,当您想扩展User.Identity的其他属性,请首先将这些属性添加到ApplicationUser类,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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; } } |
然后您需要创建一个这样的扩展方法(我在一个新的扩展文件夹中创建了我的扩展方法):
1 2 3 4 5 6 7 8 9 10 11 12 |
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类中创建标识时,只需添加Claim->OrganizationId,如下所示:
1 2 3 4 5 6 7 8 9 |
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语句:
就我而言:using App.Extensions;在控制器和@using. App.Extensions使用.cshtml视图文件。
编辑:
为了避免在每个视图中添加using语句,还可以转到Views文件夹,找到Web.config文件文件在里面。现在看看<namespaces>标记并添加扩展命名空间,如下所示:
1
|
<add namespace="App.Extensions" />
|
保存你的文件,你就完成了。现在每个视图都知道您的扩展。
您可以访问扩展方法:
1
|
var orgId = User.Identity.GetOrganizationId();
|