2014-02-14 28 views
7

我必須定製UserManager類來查找和認證公司結構中的用戶(將Active Directory身份驗證與另一個Oracle身份驗證混合)。儘管我已經實施了FindAsyncCreateIdentityAsync,但用戶並未被設置爲已通過身份驗證。OWIN - 定製UserManager

UserManager實現:

using System; 
using System.Collections.Generic; 
using System.Dynamic; 
using System.Security.Claims; 
using System.Web; 
using MyProject.Common; 
using MyProject.Models; 
using Microsoft.AspNet.Identity; 
using System.Threading.Tasks; 

namespace MyProject.Infrastructure 
{ 
    public class GNUserManager : UserManager<ApplicationUser> 
    { 
     public GNUserManager(IUserStore<ApplicationUser> store) : base(store) 
     { 

     }   

     public override async Task<ApplicationUser> FindAsync(string userName, string password) 
     { 
      /* Performs some logic here that returns true */ 

      if (foundUser) { 
       return await Task.Run(() => new ApplicationUser 
       { 
        UserName = userName, 
        Id = userName 
       }); 
      } 

      throw new Exception("User not found."); 
     } 

     public override async Task<ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string authenticationType) 
     { 
      IList<Claim> claimCollection = new List<Claim> 
      { 
       new Claim(ClaimTypes.Name, user.UserName), 
       new Claim(ClaimTypes.Country, "Brazil"), 
       new Claim(ClaimTypes.Email, user.UserName) 
      }; 

      var claimsIdentity = new ClaimsIdentity(claimCollection, "Company Portal"); 

      return await Task.Run(() => claimsIdentity); 
     } 
    } 
} 

目前缺乏已我的用戶身份驗證?

回答

1

UserManager管理數據庫中的用戶身份以及驗證憑據。簡而言之,它是一個數據庫查找工具。要讓用戶「登錄」您的應用程序,您需要發佈某種令牌(如瀏覽器應用程序的cookie或API應用程序的令牌)。 ASP.NET中最新的方法是使用用於瀏覽器應用程序的Cookie身份驗證中間件。在這裏看到更多的信息上的Cookie中間件:

http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/

+0

我昨天看到了這個網站,但它只是一瞥我正在尋找的東西。這正是我感興趣的cookie一代。 –

1

望着由ASP.NET MVC 5默認項目創建的簽到方式,我們可以看到這樣的代碼:

private async Task SignInAsync(ApplicationUser user, bool isPersistent) 
{ 
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); 
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); 
} 

我們可以注意是AuthenticationManager這是一個照顧公平的標誌,我們拿到身份後還需要用AuthenticationManager登錄。所以也許你的問題不在UserManager

控制器AuthenticationManager實例由這個碼檢索到:

private IAuthenticationManager AuthenticationManager 
{ 
    get 
    { 
     return HttpContext.GetOwinContext().Authentication; 
    } 
} 
+0

這一行:'AuthenticationManager.SignIn(new AuthenticationProperties(){IsPersistent = isPersistent},identity);'正常執行,但不生成cookie。調試此代碼我深入到'公共覆蓋異步任務 CreateIdentityAsync(ApplicationUser用戶,字符串認證類型)'。我需要了解的是如何生成cookie。 –

+0

也許你可以調用基本的方法,你最重要的方法是什麼? – iuristona

+0

曾經嘗試過,沒有結果。 –

2

嘗試改變這一行。

var claimsIdentity = new ClaimsIdentity(claimCollection, "Company Portal"); 

對此

var claimsIdentity = new ClaimsIdentity(claimCollection, DefaultAuthenticationTypes.ApplicationCookie); 

這應該生成的Cookie爲您所需要的。

1

用於.NET的Oracle數據提供程序目前不支持異步查詢和保存。