Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 名称';DefaultAuthenticationTypes';在当前上下文中不存在_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 5_Applicationmanager - Fatal编程技术网

C# 名称';DefaultAuthenticationTypes';在当前上下文中不存在

C# 名称';DefaultAuthenticationTypes';在当前上下文中不存在,c#,asp.net,asp.net-mvc,asp.net-mvc-5,applicationmanager,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Applicationmanager,我正在尝试在我的web应用程序中实现基于角色的授权,如下所示: [HttpPost] [ActionName("Login")] public ActionResult Login(LoginViewModel model) { if (ModelState.IsValid) { string userName = model.Username; string[] userRoles = (string[])Session["UserRoles"]

我正在尝试在我的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);
    }
}
我在两行上得到一个错误:

1.ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
以及:

错误1:

the name 'DefaultAuthenticationTypes' does not exist in the current context
错误2是:

Authentication manager does not contains definition for SignIn

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

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);

第二个问题已经在您的另一个问题中讨论过了

对于第一个问题,它只是一个常量字符串

如果您不想安装软件包,那么最快(也不是很正统)的解决方案是

ClaimsIdentity identity = new ClaimsIdentity("ApplicationCookie");
你可以看到定义

ClaimsIdentity identity = new ClaimsIdentity("ApplicationCookie");