【问题标题】:How to get easy access in User extended details in Identity 2.1如何轻松访问 Identity 2.1 中的用户扩展详细信息
【发布时间】:2016-11-23 11:49:50
【问题描述】:

默认身份带有一些用户属性。并且此表可以使用自定义属性完全扩展。

我添加了一个自定义属性“OrganisationId”来指定每个用户都必须是组织的一部分。

 public class ApplicationUser
    : IdentityUser<int, ApplicationUserLogin,
        ApplicationUserRole, ApplicationUserClaim>, IUser<int>
{
    public async Task<ClaimsIdentity>
        GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    public int OrganisationId { get; set; }

    public Organisation Organisation { get; set; }

    public UserProfile UserProfile { get; set; }
}

所以现在在我的大多数控制器中,我需要访问用户组织或用户组织 ID 以过滤内容。

例如:组织可以有多个地址,用户可以查看自己组织中的地址列表。

[ChildActionOnly]
    // GET: Addresses
    public ActionResult Index()
    {
        var userId = User.Identity.GetUserId<int>();

        var userOrganisationId = _organisationService.GetOrganisationByUserId(userId).Id;

        var addresses = _addressService.GetAddresses(userOrganisationId);
        var viewModel = Mapper.Map<IEnumerable<Address>, IEnumerable<AddressViewModel>>(addresses);
        return PartialView("_Index", viewModel);
    }

但我可以看到,要访问我需要在我的控制器中注入 ApplicationUserManager 或 OrganisationService 的用户组织。

在上面你看到我已经注入了 organizationService 然后创建了一个方法来查找用户组织。

我只是认为这似乎是一项非常重复的任务。

这是在不注入任何服务的情况下访问用户扩展属性的更好方法吗?

例如用户对象已经在控制器中访问。

类似:

 var organisationId = User.GetCustomProperty(u => u.OrganisationId);

或者也许使用自定义操作过滤器?

避免这种重复检查的最佳方法是什么?

谢谢,

【问题讨论】:

  • 如果您正在寻找一种从扩展方法访问OrganisationId的方法,您可以查看this
  • 是的,它奏效了。我必须自定义代码以从字符串转换为 int,但我得到了它的工作。这就是我要找的。谢谢。
  • 我很高兴它成功了。如果您需要根据他的OrganisationId 检查用户是否可以访问某些操作,那么您应该使用自定义过滤器。

标签: asp.net-mvc asp.net-mvc-5 asp.net-identity


【解决方案1】:

我发现最简单的方法是以下两行代码,希望对您有所帮助。

 public class ApplicationUser : IdentityUser
    {

        [Required]
        public string Name { 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
            return userIdentity;
        }
}

  public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

<ul class="nav navbar-nav navbar-left">
            <li>
                @{
                    var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                    var currentUser = manager.FindById(User.Identity.GetUserId());
                }
                @Html.ActionLink(currentUser.Name + " " + "Welcome", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
            </li>
            <li>
                <a href="javascript:document.getElementById('logoutForm').submit()">LogOff </a>
            </li>
        </ul>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2021-01-06
    • 2019-08-27
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    相关资源
    最近更新 更多