链接ASP.NET识别用户的用户详细表 [英] Link ASP.NET Identity users to user detail table

查看:97
本文介绍了链接ASP.NET识别用户的用户详细表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用默认的MVC模板与个别授权。运行该应用程序后,它会自动创建所需的标识表。我已成功注册的用户和所有的人都存储在 AspNetUser 表。

I've used default MVC template with individual authorization. After running the application it automatically creates the required Identity tables. I've successfully registered a few users and all of them are stored in the AspNetUser table.

现在我需要的登录的用户可以添加更多的像名字信息,最后一个名称,地址等。对于这一点,我已经创建另一个表 PersonalInformation 的柱子是什么存储这些信息。

Now what I want is the logged in user can add more information like first name, last name, address etc. For this I've created another table PersonalInformation with columns to store this information.

我也创造了 ManageController EditProfile 行动。

现在我怎么能在ASP.NET用户身份链接到这个表?我要补充一个编号列作为外键 AspNetUser ?还我怎么能成功PersonalInformation表编辑的信息存储与登录用户ID一起?

Now how can I link the ASP.NET Identity user to this table? Should I add an Id column as a foreign key to AspNetUser? Also how can I successfully store the edited information in "PersonalInformation" table along with the logged in user id?

推荐答案

您可以创建一个用户和个人信息之间的一个关系,然后利用应用程序用户管理器中创建或更新用户。

You can create one to one relationship between user and personal information and then create or update user using Application User Manager.

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;
    }

    public virtual PersonalInformation { get; set; }
}

public class PersonalInformation 
{
    [Key, ForeignKey("User")]
    public string UserId { get;set; }

    public string FirstName { get; set; }

    // other fields...

    public virtual ApplicationUser User { get;set; }
}


// Create user      
var store = new UserStore<ApplicationUser>(context);
var manager =  new ApplicationUserManager(store);
var user = new ApplicationUser() { Email = "email@email.com", UserName = "username", PersonalInformation = new PersonalInformation { FirstName = "FirstName" } };
manager.Create(user, "Password123!");

// Update user
var store = new UserStore<ApplicationUser>(context);
var manager =  new ApplicationUserManager(store);
var user = manager.Users.FirstOrDefault(u => u.Id == id);
user.PersonalInformation.FirstName = "EditedName";
manager.Update(user);

这篇关于链接ASP.NET识别用户的用户详细表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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