【发布时间】:2016-07-03 11:06:30
【问题描述】:
我正在尝试使用 OWIN 为 MVC5 推出自己的身份验证。我想避免使用标准的 .NET 身份 + EF 内容,因为我正在重写现有网站的 Web 层并保持底层数据库完好无损(它使用了自定义表单身份验证提供程序,bcrypt 用于密码等) .我目前无法让我的用户通过身份验证。这是我目前拥有的:
Startup.cs:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
LoginPath = new PathString("/account/login")
});
}
}
AccountController.cs:
public class AccountController : Controller
{
private IAuthenticationManager authenticationManager
{
get
{
return this.HttpContext.GetOwinContext().Authentication;
}
}
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model)
{
if (!ModelState.IsValid)
return View();
var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
identity.AddClaim(new Claim(ClaimTypes.Name, model.Email));
identity.AddClaim(new Claim(ClaimTypes.Email, model.Email));
this.authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = model.RememberMe }, identity);
return RedirectToAction("Index", "Home");
}
public ActionResult LogOff()
{
this.authenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
}
现在,我的 Home/Index 操作用 [Authorize] 属性装饰。这似乎起作用,因为当我导航到该页面时,我被推送到登录页面。这正确地回发并调用IAuthenticationManager.SignIn 方法,然后将我重定向到主页。但是,此时,我只是再次被重定向回登录页面,这表明我的用户实际上还没有登录。我已经编写了自己的 WebAPI 身份验证处理程序(标头中的 API 密钥身份验证等),它们是自定义中间件,但网上的很多信息表明以下内容足以用于 MVC 中的身份验证。有什么想法我可能会出错吗?
【问题讨论】:
标签: c# asp.net-mvc authentication asp.net-mvc-5 owin