在标识2.0扩展IdentityUserRole [英] Extending IdentityUserRole in Identity 2.0

查看:90
本文介绍了在标识2.0扩展IdentityUserRole的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的系统需要角色有关联的到期日期。我已经实现了身份2.0框架,事情进展顺利,但我碰到的就是让我怀疑我的结构的问题。

So my system requires that Roles have associated Expiry dates. I have implemented the Identity 2.0 framework, and things are going smoothly, but I've run into an issue that is making me doubt my structure.

public class ApplicationUserRole : IdentityUserRole
{
    public override string UserId { get; set; }
    public override string RoleId { get; set; }
    public DateTime Expiry { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }
    static ApplicationDbContext()
    {
        Database.SetInitializer<ApplicationDbContext>(null);
    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public DbSet<ApplicationUserRole> ApplicationUserRoles { get; set; }
}

这做什么,我想它。因此,它增加了第三列是一个名为到期日期时间它改变了AspNetUserRoles表。我能够添加,更新,并使用ApplicationDBContext删除到期,一切工作正常和良好,而编辑的用户。

This does what I want it to. It changes the AspNetUserRoles table so that it adds a third column which is a DateTime called Expiry. I'm able to add, update, and remove expiries using the ApplicationDBContext, all works fine and well while editing Users.

问题出现在帐户创建。该呼吁的UserManager AddToRolesAsync,但只需要两个参数,用户和角色。我需要它采取用户,角色和到期。

The issue arises on Account Creation. The UserManager calls AddToRolesAsync, but only takes two parameters, User and Role. I need it to take User, Role, and an Expiry.

我需要实现我自己的UserManager?这似乎有点小题大做。

Do I need to implement my own UserManager? That seems like overkill.

我能不容许在帐户创建角色/到期选择,只是离开它到编辑模式来解决这个问题。但是,我真的希望能够创建帐户,并在同一时间分配角色/到期。

I'm able to get around the issue by not allowing role/expiry selection on account creation, and just leaving it to edit mode. But I'd really like to be able to create accounts and assign roles/expiries at the same time.

推荐答案

看看下面的视频教程使用现有的数据库表使用asp.net的身份。我认为你必须让ApplicationUser表知道你在ApplicationUserRole表中有新的领域。所以,你必须遵循的实体框架模型,以便结合实现这一目标。

Take a look at below video tutorial on using asp.net identity using existing database tables. I think you have to let ApplicationUser table know that you have new field in your ApplicationUserRole table. So that you have to follow entity framework model binding in order to achieve that.

第1部分 - https://www.youtube.com/watch?v=elfqejow5hM

第二部分 - https://www.youtube.com/watch?v=JbSqi3Amatw

您不必实现自己的UserManager,但你必须修改它。希望你可以用这个例子来支持您的需求。

You don't need to implement your own userManager, but you have to amend it. Hope you can use this example to support your needs.

这篇关于在标识2.0扩展IdentityUserRole的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆