我正在尝试使用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 auth处理程序(标题中的API密钥auth等),它们是自定义中间件,但网上的大量信息表明以下内容足以在MVC中进行身份验证。我可能会出错的任何想法?
答案 0 :(得分:1)
我刚写完这个,我立即发现了问题:我的OWIN启动类和帐户控制器的身份验证类型不同。改变为相同,完美地运作。