简体   繁体   English

具有asp.net身份的asp.net Web API 2身份验证

[英]Asp.net web API 2 authentication with asp.net Identity

I have Solution consisting of 3 projects, first is Data Access Layer (where models and Database Context are defined), and other two are Asp.net MVC 5 and Asp.net Web Api 2 projects. 我的解决方案由3个项目组成,第一个是数据访问层(定义了模型和数据库上下文),另外两个是Asp.net MVC 5和Asp.net Web Api 2项目。

For authentication and authorization I'm using Asp.net Identity which I've set up like this (in my DAL project): 为了进行身份验证和授权,我使用的是这样设置的Asp.net Identity(在我的DAL项目中):

public class DatabaseContext : IdentityDbContext<User>
{
    public DatabaseContext()
        : base("DefaultConnection")
    {
        Configuration.LazyLoadingEnabled = true;
    }

    public DbSet<IdentityUser> IdentityUsers { get; set; }

    /*
        Other DbSets ...
    */
}

My User class extends IdentityUser. 我的用户类扩展了IdentityUser。

In my ASP.net project I've changed a bit Account controller so it works with my Database Context, here are relevant parts which I changed: 在我的ASP.net项目中,我更改了一些Account控制器,以便它与我的Database Context一起使用,这是我更改的相关部分:

[Authorize]
public class AccountController : Controller
{
    public AccountController()
        : this(new UserManager<User>(new UserStore<User>(new DatabaseContext())))
    {
    }

    public AccountController(UserManager<User> userManager)
    {
        UserManager = userManager;
    }

    public UserManager<User> UserManager { get; private set; }
}

And this part works fine. 这部分工作正常。 So my question is, what changes I have to do in order my Web API project works with my entity User and my Database Context, so in order to consume API user must be logged in ? 所以我的问题是,为了使我的Web API项目与我的实体User和我的Database Context一起使用,我必须做些什么更改,以便使用API​​用户必须登录? (and of-course user has option to register via API). (当然,用户可以选择通过API注册)。

IE I want to enable authorization and authentication for web api 2 by using my Database Context and class User which extends Identity User class. IE浏览器,我想通过使用我的数据库上下文和扩展身份用户类的用户类来启用Web api 2的授权和身份验证。

UPDATE UPDATE

What I've tried to do is this: I've replaced all IdentityUser classes in my web api 2 project with my class User, but on line (when I try to log in): 我想做的是:我用用户类替换了Web api 2项目中的所有IdentityUser类,但是在线(当我尝试登录时):

User user =  await userManager.FindAsync(context.UserName, context.Password);

I get error: 我得到错误:

System.Data.SqlClient.SqlException: Invalid object name 'dbo.AspNetUsers'. System.Data.SqlClient.SqlException:无效的对象名称'dbo.AspNetUsers'。

This approach worked for my asp.net MVC project. 这种方法适用于我的asp.net MVC项目。

In short what I did was: 简而言之,我所做的是:

In my DatabaseContext class I've added these lines: 在我的DatabaseContext类中,添加了以下几行:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>()
            .ToTable("AspNetUsers");
        modelBuilder.Entity<User>()
            .ToTable("AspNetUsers");
    }

In asp.net web api project I've changed all classes IdentityUser to User, and that's it 在asp.net Web API项目中,我将所有类IdentityUser更改为User,仅此而已

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM