我已经添加了这些用法,并且我安装了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();
}
}
}
答案 0 :(得分:3)
尝试在代码中添加:
using System.Security.Claims;