当前上下文中不存在名称"DefaultAuthenticationTypes"

Use*_*987 6 c# asp.net asp.net-mvc applicationmanager asp.net-mvc-5

我正在尝试在我的Web应用程序中实现基于角色的授权,如下所示:

[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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在两行上出错了:

1.ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
Run Code Online (Sandbox Code Playgroud)

和:

2.AuthenticationManager.SignIn(identity);
Run Code Online (Sandbox Code Playgroud)

错误1:

the name 'DefaultAuthenticationTypes' does not exist in the current context
Run Code Online (Sandbox Code Playgroud)

错误2是:

Authentication manager does not contains definition for SignIn
Run Code Online (Sandbox Code Playgroud)

我试图找到一个解决方案如何实现这个,但我找不到任何与错误相关的内容.

Nko*_*osi 6

DefaultAuthenticationTypes是Identity框架的一部分,可在Microsoft.AspNet.Identity命名空间中找到.

要使用它,请using在文件顶部添加一个

using Microsoft.AspNet.Identity;
//...other code
identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
Run Code Online (Sandbox Code Playgroud)

或直接打电话

identity = new ClaimsIdentity(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
Run Code Online (Sandbox Code Playgroud)

第二个问题在另外一个你的问题已经处理了这里