2017-09-19 18 views
0

我想在我的MVC項目中實現身份驗證。問題是我已經實現了ASP.NET Identity OWIN。我可以成功進行身份驗證,並且一切都很好。ASP.NET MVC身份驗證從IdentityUser轉換爲IIdentity

的問題是,有檢索經由System.Threading.Thread.CurrentPrincipal.Identity

經認證的用戶在該服務層其他項目(如服務層)的值被澆鑄的自定義類如下:

public MyCompanyIdentity Identity 
    { 
     get { return System.Threading.Thread.CurrentPrincipal.Identity as MyCompanyIdentity ; } 
    } 

的MyCompanyIdentity類的內容如下:

public class MyCompanyIdentity : IIdentity 
    { 
     public MyCompanyIdentity(); 

     public string EndUserName { get; set; } 
     public string Exception { get; set; } 
     public string Ticket { get; set; } 
     public string Company { get; set; } 
     public string Fullname { get; set; } 
     public string Email { get; set; } 
     public bool IsAuthenticated { get; } 
     public string Name { get; } 
     public string AuthenticationType { get; } 
    } 

在我的控制器I類設置身份驗證弗洛女:

var userStore = new UserStore<DemoApplicationUser>(new PeopleSaaIdentityContext()); 
var userManager = new UserManager<DemoApplicationUser>(userStore); 
var user = userManager.Find(username, password); 

if (user != null) 
{ 
    var authManager = HttpContext.Current.GetOwinContext().Authentication; 
    ClaimsIdentity userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); 
    authManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties() { IsPersistent = true }, userIdentity); 
    //How do I cast/convert the System.Threading.Thread.CurrentPrincipal.Identity to a MyCompanyIdentity?            
} 

既然不能改變MyCompanyIdentity實施IdentityUser,因爲我沒有訪問源代碼。

如何「投/轉換」到IIdenityUser?我想重複使用它在應用程序中隨處可見的類。

我真的希望這是有道理的,別讓我知道,我會嘗試進一步解釋。

回答

0

除了從零開始重寫邏輯之外,它別無選擇。