我在一个ASP MVC登录表单上工作。为什么我的microsoft.owin.security.authenticationmanager登录方法不起作用?
我有非常简单的代码。 A Startup
类和尝试设置Cookie的操作。下面是我的代码:
启动 它位于App_Start(也有与key="owin:AppStartup"
对它的引用在<appSetting>
)
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "ApplicationCookie",
LoginPath = new PathString("/auth/login"),
});
}
}
被假设验证用户是该动作的方法:
[HttpPost]
public ActionResult Login(user model)
{
if(ModelState.IsValid)
{
var identity = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Email, "[email protected]"),
new Claim(ClaimTypes.Name, "tom"),
new Claim(ClaimTypes.Role, "admin")
});
var ctx = Request.GetOwinContext();
var authManager = ctx.Authentication;
authManager.SignIn(identity);
return RedirectToAction("Index", "Home");
}
return View(model);
}
但这没有得到身份认证为@User.Authenticated
在我的_Layout.cshtml
中是错误的,当return RedirectToAction("Index", "Home");
和debbuger显示IsAuthenticated
属性为false(在控制器Login
动作中和在_Layout.cshtml
中。
我已检查IIS是否为匿名身份验证使用我的Windows管理工具启用,还我已经检查了启动设置应用程序启动时...
我看来,authManager.SignIn(identity)
没有做的工作。
我们该如何解决?
PS:我甚至不看到浏览器弹出,询问我是否保存密码