网页API 2 OWIN OAuth的承载令牌 [英] Web Api 2 with OWIN OAuth Bearer tokens

查看:231
本文介绍了网页API 2 OWIN OAuth的承载令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在建立在Visual Studio 2013的Web API的过程,并想用OWIN中间件和承载令牌进行身份验证。不过我已经有一个数据库,并且不希望使用微软的新标识框架作为广大的表和列,它自动生成我根本就不需要。

I'm in the process of building a web api in visual studio 2013 and want to authenticate using OWIN middleware and bearer tokens. However I already have a database and don't want to use Microsoft's new Identity framework as the majority of tables and columns that it auto generates I simply don't need.

任何人都可以点我如何应用这种类型的身份验证的正确方向的没有不必使用Microsoft身份的框架?

Can anyone point me in the right direction of how to apply this type of authentication without having to use the Microsoft Identity framework?

推荐答案

我的建议是使用框架,但它扩大到用你的对象和基础设施。我目前在做这中间,落在了这个问题。以下是我到目前为止已经解决它:

My suggestion would be to use the framework but extend it to use your objects and infrastructure. I am currently in the middle of doing this and landed on this question. Here's how I've tackled it so far:

第1步:您自己CustomUserObject

Step 1: Your own CustomUserObject

写/用你自己的ApplicationUser对象。在模板项目,要修改IdentityModels文件。它在那里定义ApplicationUser对象。假设你已经从你现有的应用程序的所有属性,你将需要添加GenerateUserIdentityAsync()方法,但修改参数来的UserManager经理的类型)。变更后,你的方法签名是这样的:

Write/Use your own "ApplicationUser" object. In the template project, you want to modify the "IdentityModels" file. It has ApplicationUser object defined in there. Assuming you already have all the properties from your existing app, you will need to add GenerateUserIdentityAsync() method but change the type of the parameter to UserManager manager). After the change, your method signature looks like this:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<CustomUserObject> manager)

第2步:定义你自己的IUserStore&LT;>实施

Step 2: Define your own IUserStore<> implementation

添加一个新类CustomUserStore实现IUserStore,像这样:

Add a new class CustomUserStore that implements IUserStore, like so:

public class CustomUserStore : IUserStore<CustomUserObject>
{
    private readonly IUserManagerService _userManagerService;
    public CustomUserStore(IUserManagerService userManagerService)
    {
        _userManagerService = userManagerService
    }

    //implementation code for all of the IUserStore methods here using
    //userManagerService or your existing services/classes
}

我使用统一注入上述IUserManagementService的实现。

I am using Unity to inject IUserManagementService's implementation above.

我刚才所用自带的Microsoft身份框架COM prehensive的UserManager类的而是扩大到使用我的API进行身份验证和授权的框架。你可以写你自己的UserManager但我发现,这是pretty乏味和没有理由的UserManager可以为保护应用程序的大多数情况下工作。

I have made use of the comprehensive UserManager class that comes with the Microsoft Identity framework but extended the framework to use my API for authentication and authorization. You could write your own UserManager but I found that it is pretty tedious and there is no reason why UserManager could work for most cases of Securing an app.

步骤3:在IdentityConfig.cs文件更改

Step 3: Changes in the IdentityConfig.cs file

更改类定义的UserManager从做ApplicationUserManager类继承

Change the class definition to make ApplicationUserManager class inherit from UserManager

您需要做的在这个类的构造函数的samething为好;即有IUserStore。修改创建静态方法的第一线,利用新的商店和一个包装类,它提供的是一个的DbContext像这样的手段:

You'll need to do the samething in the constructor of this class as well; i.e. have IUserStore. Modify the Create static method's first line to make use of the new store and a wrapper class that provides as a means to be a "DbContext" like so:

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    {
        var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<UserManagementServiceWrapper>()));
        //modify the relevant lines after this to suit your needs
        ...
    }

我UserManagementServiceWrapper看起来像这样(请注意,我不是太高兴,这从一个具体的UserManagementService类,它提供的方法连接到提供用户数据,我还在建设这个出来的服务继承):

My UserManagementServiceWrapper looks like this (please note that I'm not too happy that it inherits from a concrete UserManagementService class that provides the methods to connect to the service that provides user data, I'm still building this out):

public class UserManagementServiceWrapper : UserManagementService, IDisposable
{
    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

第4步:更改ApplicationDbContext类返回UserManagementServiceWrapper实例

Step 4: Change the ApplicationDbContext class to return a UserManagementServiceWrapper instance

public class ApplicationDbContext : UserManagementServiceWrapper
{
    public static UserManagementServiceWrapper Create()
    {
        return new UserManagementServiceWrapper();
    }
}

这是pretty多少呢。你仍然需要编写CustomUserStore目标的实现,但一切都应该工作。

And that is pretty much it. You still have to write the implementation for CustomUserStore object but everything should work.

请注意,这不是样板code和没有在附近code复习准备好了,正如我所说,我还在更深的挖掘到这一点,并建立它使用自定义存储,数据访问对象,服务等等。我还以为你会得到一些的我花了几个小时,以找出事情一个良好的开端。我将博客这个时候我有一个很好的解决方案。

Please note this is not boilerplate code and no where near "code review ready", as I said, I'm still digging deeper into this and building it out to use custom stores, data access objects, services etc. I thought you'll get a good start with some of the things that took me a couple of hours to figure out. I will blog about this when I have a good solution.

希望这有助于。

这篇关于网页API 2 OWIN OAuth的承载令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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