如何扩展asp.net web api 2 用户? [英] How to extend asp.net web api 2 user?

查看:18
本文介绍了如何扩展asp.net web api 2 用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Web API (2) 项目并使用 个人帐户"身份验证.我想

I'm building a Web API (2) project and use "individual account" authentication. I want to

  1. 用一些细节扩展用户(如名字/姓氏等) (在模型中)
  2. 登录后获取该信息(在客户端 - 我的案例 Windows 手机)

我刚刚开始学习 MVCWeb API,所以有人可以帮我吗?

I've just started learning MVC and Web API so can anybody help me on this?

让我解释一下自己:我已经创建了 webapi 项目并选择了身份验证方法 - 个人帐户"好吧,我添加了一个名为 Person 的模型类,其中包含 2 个字段:FirstName 和 LastName.好吧,我已经使用 fiddler 用密码注册了一个名为johndoe"的用户.在此之后,我使用 fiddler 在server:port/Token"上对该用户进行身份验证并获得了不记名令牌.那么这里有一个问题,我需要知道如何将这个用户类与我在模型中的 Person 类相关联,其次我不知道如何编写一个控制器,所以什么时候我们的控制器函数会发送一个获取请求,我们的控制器函数将返回关联的 Person.

Let me explain myself a little bit: I have create webapi project and selected as authentication method - "inidividual accounts" well I've added a model class called Person with 2 fields: FirstName and LastName. Well I've used fiddler to register a user called "johndoe" with a password. After this I've used fiddler to authenticate this user at "server:port/Token" and got bearer token. Well Here is a problem, I need to know how to associate this user class with my Person class in model, and second I do not know how to write a controller so when will send a get request our controller function will return associated Person.

推荐答案

首先,如:ASP.NET Identity 简介

默认情况下,ASP.NET Identity 系统将所有用户信息存储在数据库中.ASP.NET Identity 使用 Entity Framework Code First 来实现其所有持久性机制.

By default, the ASP.NET Identity system stores all the user information in a database. ASP.NET Identity uses Entity Framework Code First to implement all of its persistence mechanism.

现在,IdentityUser 类将映射到数据库中的 AspNetUsers 表,因此让我们通过创建一个继承自它的新类并添加我们的自定义属性来扩展它:

Now,the IdentityUser class will be mapped to AspNetUsers Table in Database,so lets extend it by create a new class that inherit from it and add our custom properties:

public class CustomUser : IdentityUser
{
    public string Email { get; set; }
}

那么你应该让 UserManager 类使用 CustomUser 而不是 IdentityUser 类,所以在 Startup.cs 中,将 UserManager 和 UserStore 的通用类型更改为:

Then you should let the UserManager class to use CustomUser instead of IdentityUser Class,so in Startup.cs ,change the generic type for UserManager and UserStore to be like:

UserManagerFactory = () => new UserManager<CustomUser>(new UserStore<CustomUser>());

还有属性:

public static Func<UserManager<CustomUser>> UserManagerFactory { get; set; }

在 ApplicationOAuthProvider.cs 和解决方案中的任何地方对 UserManager 的通用类型进行相同的更改,并开始使用 CustomUser 而不是 IdentityUser .您可以将新的电子邮件"属性添加到 RegisterBindingModel 为:

In ApplicationOAuthProvider.cs and any place in your solution do the same changes for generic type for UserManager,and start using CustomUser instead of IdentityUser . You can add the new "Email" property to RegisterBindingModel as :

public class RegisterBindingModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    public string Email { get; set; }
}

然后在注册中你可以增加价值:

Then in registration you can add value:

 CustomUser user = new CustomUser
        {
            UserName = model.UserName,
            Email=model.Email
        };

希望能帮到你

更新

如果有人收到 ExceptionMessage无效的列名‘鉴别器’."这意味着在按照我的步骤之前已经创建了AspNetUsers",并且表没有鉴别​​器"列,所以你应该做的就是去数据库,通过添加一个新的列来改变AspNetUsers表的设计类型作为 nvarchar(128)(在我的示例中还添加一个您想要的新列,例如 Email),并将其命名为 Discriminator ,并且不要忘记使用适当的值填充旧记录的此列的字段(在我的例如它将是 CustomUser).

If anyone got an ExceptionMessage "Invalid column name 'Discriminator'." that means the "AspNetUsers" is already created before following my steps ,and the table doesn't have "Discriminator" column, so all what you should do is going to database,change the design of AspNetUsers table by adding a new column with type as nvarchar(128)(Also add a new column that you want to have like Email in my example),and name it Discriminator , and don't forget to fill the fields of this column for old records with the appropriate value (in my example it will be CustomUser).

这篇关于如何扩展asp.net web api 2 用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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