2013-11-25 49 views
0

假設我有一個隨機接受用戶的網站。規則很簡單;隨機生成器可以告訴用戶可以登錄或不登錄。使用新的OWIN標識API登錄

那麼應該如何使用這段代碼呢?

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

我沒有ApplicationUser,我沒有注入DbContext,我沒有用戶(!)表。

主要問題;我的簡單身份驗證在哪裏?

FormsAuthentication.RedirectFromLoginPage ("I_am_a_User", Persist.Checked) 

回答

0

代碼看起來很醜,但我解決了......

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 

     List<Claim> _claims = new List<Claim>() 
           { 
            new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", 
             Guid.NewGuid().ToString(), 
             "http://www.w3.org/2001/XMLSchema#string", "LOCAL AUTHORITY"), 

            new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", 
             "I_am_a_User", 
             "http://www.w3.org/2001/XMLSchema#string", "LOCAL AUTHORITY"), 

            new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", 
             "ASP.NET Identity", 
             "http://www.w3.org/2001/XMLSchema#string", "LOCAL AUTHORITY"),           
           }; 
     ClaimsIdentity identity = new ClaimsIdentity(_claims, DefaultAuthenticationTypes.ApplicationCookie); 

AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);