类型或命名空间名称' ClaimsIdentity'无法找到

时间:2015-04-28 02:16:37

标签: c# asp.net

我已经添加了这些用法,并且我安装了nuget软件包。为什么我不能使用索赔? Visual Studio说无法找到ClaimsIDentity,Claim和ClaimTypes。

〜/控制器/ AccController.cs

using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace MyProject.Controllers
{
    public class AccController : Controller
    {
        [AllowAnonymous]
        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }

            // Don't do this in production!
            if (model.Email == "admin@admin.com" && model.Password == "password")
            {
                var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimTypes.Name, "Ben"),
                new Claim(ClaimTypes.Email, "a@b.com"),
                new Claim(ClaimTypes.Country, "England")
            },
                    "ApplicationCookie");

                var ctx = Request.GetOwinContext();
                var authManager = ctx.Authentication;

                authManager.SignIn(identity);

                return Redirect(GetRedirectUrl(model.ReturnUrl));
            }

            // user authN failed
            ModelState.AddModelError("", "Invalid email or password");
            return View();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

尝试在代码中添加:

using System.Security.Claims;