我是OWIN身份验证的新手,我正试图让(看似)基本工作。
当我发布到我的登录操作时,“_ signInManager.PasswordSignInAsync”会返回成功,但我的IPrincipal用户似乎没有更新。另外,如果我执行单独的测试(I.E执行userManager.FindByEmail并对登录进行计数,或尝试SignInManager.GetVerifiedUserId),我没有获得任何成功登录尝试的历史记录。因此,当重定向发生时.net已经忘记了登录用户,并且表现得好像从未经过身份验证,我不知道为什么。
值得注意的是,我的Identity是一个独立的解决方案,由主要解决方案引用。
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
public AccountController()
{
_userManager = OwinContext.GetApplicationUserManager();
_signInManager = OwinContext.GetApplicationSignInManager();
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
***Other methods***
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(Login model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (OwinContext.SignInResult(result))
{
case OwinContext.SignInStatus.Success:
if (User.IsInRole("Admin") || User.IsInRole("SuperAdmin"))
{
return RedirectToAction("UserList");
}
return RedirectToLocal(returnUrl);
case OwinContext.SignInStatus.LockedOut:
return View("Lockout");
case OwinContext.SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case OwinContext.SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
private static IOwinContext GetOwinContext()
{
return HttpContextWrapper.GetCurrentContext().GetOwinContext();
}
public static ApplicationSignInManager GetApplicationSignInManager()
{
var applicationSignInManager = GetOwinContext().Get<ApplicationSignInManager>();
return applicationSignInManager;
}
using System.Data.Entity;
using Microsoft.Owin;
using Owin;
using Shared_Identity.IdentityConfig;
using Shared_Identity.Models.Context;
namespace Shared_Identity
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
//ConfigureAuth(app);
Database.SetInitializer<SharedIdentity>(null);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
}
}
}
我试过以下内容:
https://stackoverflow.com/a/27282956/4046026
https://stackoverflow.com/a/30734550/4046026 - (确认数据库连接字符串正确)
https://stackoverflow.com/a/24643382/4046026
https://stackoverflow.com/a/27500097/4046026
看看谷歌的一些misc教程,但到处似乎都认为这应该是直截了当的。我可能遗漏了一些简单或有概念性问题。
答案 0 :(得分:2)
具体做法是:
您需要在Startup.Auth.cs中进行以下更改:
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});