2016-10-28 64 views
3

我試圖實現基於角色的授權在我的web應用程序像下面:「DefaultAuthenticationTypes」這個名字不會在目前情況下存在

[HttpPost] 
[ActionName("Login")] 
public ActionResult Login(LoginViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     string userName = model.Username; 
     string[] userRoles = (string[])Session["UserRoles"]; 

     ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); 

     identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName)); 

     userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role))); 

     identity.AddClaim(new Claim(ClaimTypes.Name, userName)); 

     AuthenticationManager.SignIn(identity); 

     return RedirectToAction("Success"); 
    } 
    else 
    { 
     return View("Login",model); 
    } 
} 

我在兩行得到一個錯誤:

1.ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); 

和:

2.AuthenticationManager.SignIn(identity); 

錯誤1:

the name 'DefaultAuthenticationTypes' does not exist in the current context 

和錯誤2:

Authentication manager does not contains definition for SignIn 

我試圖找到一個解決方案如何實現這一點,但我找不到相關的錯誤東西。

回答

3

DefaultAuthenticationTypes是Identity Framework的一部分,在Microsoft.AspNet.Identity命名空間中找到。

要使用它,添加using到文件

using Microsoft.AspNet.Identity; 
//...other code 
identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); 

的頂部或直接調用它

identity = new ClaimsIdentity(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie); 

第二個問題您的問題另外一個已經處理了here

相關問題