如何延长asp.net网页API 2的用户? [英] How to extend asp.net web api 2 user?

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

问题描述

我建立一个的Web API(2)项目并使用个人账户认证
我想

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


  1. 扩展用户有一些细节的(如姓/名等)(模型)

  2. 获得登录后,该信息的(在客户端 - 我的情况的Windows Phone)

  1. extend user with some details (like first name/last name, etc) (in model)
  2. obtain that info after login (on client side - my case windows phone)

我刚开始学习 MVC 的Web API 让任何人可以帮助我在这?

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

让我解释一下我自己一点点:
我已创建的WebAPI项目,并选择身份验证方法 - inidividual帐户
好,我已经添加名为Person模型类有2个字段:名字和姓氏。嗯,我已经使用小提琴手注册了一个名为为johndoe带密码的用户。在此之后我用小提琴手该用户在验证服务器:端口/令牌,获得承载令牌。那么这里有一个问题,我需要知道如何给该用户类模型我Person类相关联,第二我不知道怎么写控制器,所以当将发送GET请求我们的控制器函数将返回相关的人。

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.

推荐答案

首先,如:<一href=\"http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity\">Introduction到ASP.NET身份

默认情况下,ASP.NET识别系统在数据库中存储的所有用户信息。 ASP.NET身份使用实体框架code首先实现所有它的持久性机制。

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

我希望这将有助于

I hope it will help

更新

如果任何人有一个ExceptionMessage无效列名'鉴'。这意味着AspNetUsers按照我的步骤之前已经创建,并且表没有鉴别​​一栏,所以你应该做的是要数据库,通过添加类型的新列更改AspNetUsers表的设计如为nvarchar(128)(另外补充一点,你想有像在我的例子电子邮件的新列),并将其命名为鉴,不要忘记填写此列的旧记录与适当的值的字段(在我例如,它会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网页API 2的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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