具有现有Users表的Asp.Identity

时间:2017-03-01 13:58:25

标签: asp.net sql-server asp.net-mvc asp.net-identity asp.net-membership

我需要为成员准备一个带有现有tbl_users表的MVC 5应用程序。我可以使用或修改Asp.Identity还是需要准备自定义会员资格? 请指教。

CREATE TABLE [dbo].[tbl_user](
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [user] [varchar](50) NOT NULL,
    [password] [varchar](60) NOT NULL,
    [password_raw] [varchar](255) NULL,
    [name] [varchar](255) NOT NULL,
    [email] [varchar](255) NOT NULL,
    [url] [varchar](255) NULL,
    [language] [varchar](255) NULL,
    [active] [smallint] NOT NULL,
    [session_id] [varchar](100) NULL,
    [reg_date] [datetime2](0) NOT NULL,
    [theme] [varchar](255) NULL,
    [timezone] [varchar](255) NULL,
    [last_visit_date] [datetime2](0) NULL,
    [past_visit_date] [datetime2](0) NULL,
    [activation] [varchar](32) NULL,
    [activation_date] [datetime2](0) NULL,
    [ip_address] [varchar](255) NULL,
    [security_question] [varchar](255) NULL,
    [security_answer] [varchar](255) NULL,
    [lock_url] [varchar](500) NULL,
    [lock_is_locked] [smallint] NOT NULL,
    [lock_time] [datetime2](0) NULL,
    [change_password] [smallint] NOT NULL,
    [last_change_password_date] [datetime2](0) NOT NULL,
    [expire_date] [date] NULL,
    [employee_no] [bigint] NULL,
    [student_no] [bigint] NULL,
    [parent_no] [bigint] NULL,
    [user_type] [varchar](5) NULL,
    [last_site] [varchar](50) NULL

谢谢。

1 个答案:

答案 0 :(得分:2)

您可以从IUser派生自定义用户类,并在那里定义属性(附加组件)。像这样:

public class IdentityUser: IUser
{
    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="IdentityUser"/> class
    /// </summary>
    public IdentityUser()
    {
        Id = Guid.NewGuid().ToString();
        Roles = (ICollection<IdentityUserRole>)new List<IdentityUserRole>();
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="IdentityUser"/> class
    /// </summary>
    /// <param name="username">Username for the user</param>
    public IdentityUser(string username): this()
    {
        UserName = username;
    }

    #endregion

    #region IUser<string> members
    // Custom properties...
    #endregion
}

您可以在此处找到更多信息:https://jinishbhardwaj.wordpress.com/2014/07/16/decoupling-asp-net-identity-2-0-from-entity-framework-in-mvc5-part-1/