簡體   English   中英

帶有OWIN OAuth承載令牌的Web Api 2

[英]Web Api 2 with OWIN OAuth Bearer tokens

我正在使用Visual Studio 2013構建web api,並希望使用OWIN中間件和持有者令牌進行身份驗證。 但是我已經有了一個數據庫,並且不想使用Microsoft的新Identity框架作為它自動生成的大多數表和列,我根本不需要。

任何人都可以指出我如何應用此類身份驗證的正確方向, 無需使用Microsoft身份框架?

我懷疑。 不完全理解這個問題,但看起來你似乎沒有整個owin管道嗎?

如果不是那么..

您需要實現與用戶和角色相關的幾個接口,如下所述。

http://www.asp.net/identity/overview/extensibility/overview-of-custom-storage-providers-for-aspnet-identity

看看Scott Allen的以下帖子

http://odetocode.com/blogs/scott/archive/2013/11/25/asp-net-core-identity.aspx

這樣,您可以使用自己的表,DAL和服務來創建UserManager和RoleManager對象。

編輯: 這里的樣本應該給你一些指示。

Edit2:自定義用戶存儲示例。 IRepository是處理CRUD的對象。

    public class CustomUserStore : IUserStore<User>,....
    {
        private readonly IRepository _repository;
        public CustomUserStore(IRepository repository)
        {
            if (repository == null)
                throw new ArgumentNullException("repository");
            _repository = repository;
        }
        public Task CreateAsync(User user)
        {
            if (user == null) throw new ArgumentException("user");
            _repository.User.Add(user);
            return _repository.CommitAsync();
        }
...

我的建議是使用框架,但擴展它以使用您的對象和基礎結構。 我目前正在做這件事並着手解決這個問題。 到目前為止,我已經解決了這個問題:

第1步:您自己的CustomUserObject

編寫/使用您自己的“ApplicationUser”對象。 在模板項目中,您要修改“IdentityModels”文件。 它具有在那里定義的ApplicationUser對象。 假設您已經擁有現有應用程序中的所有屬性,則需要添加GenerateUserIdentityAsync()方法,但將參數類型更改為UserManager manager。 更改后,您的方法簽名如下所示:

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

第2步:定義您自己的IUserStore <>實現

添加一個實現IUserStore的新類CustomUserStore,如下所示:

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
}

我正在使用Unity注入上面的IUserManagementService實現。

我已經使用了Microsoft Identity框架附帶的綜合UserManager類,但擴展了框架以使用我的API進行身份驗證和授權。 您可以編寫自己的UserManager,但我發現它非常繁瑣,並且沒有理由說UserManager可以用於大多數保護應用程序的情況。

第3步:IdentityConfig.cs文件中的更改

更改類定義以使ApplicationUserManager類繼承自UserManager

你還需要在這個類的構造函數中做同樣的事情; 即擁有IUserStore。 修改Create static方法的第一行以使用新存儲和一個包裝類,它提供了一個“DbContext”的方法,如下所示:

    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類,該類提供連接到提供用戶數據的服務的方法,我仍在構建它):

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

步驟4:更改ApplicationDbContext類以返回UserManagementServiceWrapper實例

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

這就是它。 您仍然必須編寫CustomUserStore對象的實現,但一切都應該工作。

請注意,這不是樣板代碼,並且沒有“代碼審查准備就緒”附近,正如我所說,我仍然在深入研究這個問題並將其構建成使用自定義商店,數據訪問對象,服務等。我以為你會從我花了幾個小時才能弄明白的一些事情開始。 當我有一個很好的解決方案時,我會在博客上寫這個。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM