名称"DefaultAuthenticationTypes"在当前上下文中不存在 [英] The name 'DefaultAuthenticationTypes' does not exist in the current context
本文介绍了名称"DefaultAuthenticationTypes"在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在我的Web应用程序中实现基于角色的授权,如下所示:
I'm trying to implement role-based authorization in my web application like following:
[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
我试图找到一种解决方案,但我找不到与错误有关的任何东西.
I was trying to find a solution how to implement this but I couldn't find anything related to the errors.
推荐答案
DefaultAuthenticationTypes
是Identity框架的一部分,位于Microsoft.AspNet.Identity
命名空间中.
DefaultAuthenticationTypes
is part of Identity framework and found in Microsoft.AspNet.Identity
namespace.
要使用它,请在文件顶部添加using
To use it, add a using
to the top of the file
using Microsoft.AspNet.Identity;
//...other code
identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
或直接调用
identity = new ClaimsIdentity(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
第二个问题已经在您的另一个问题中在这里
The second issue was already dealt with in another one of your questions here
这篇关于名称"DefaultAuthenticationTypes"在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文