我刚开始使用OWIN身份验证,我正在尝试获得(似乎)基本的工作基础。
但是,当我在登录操作中发布"_signInManager.PasswordSignInAsync“返回成功时,我的IPrincipal用户似乎没有被更新。此外,如果我执行单独的测试(即执行userManager.FindByEmail并对登录进行计数,或者尝试SignInManager.GetVerifiedUserId),我就不会获得任何成功登录尝试的历史记录。因此,当重定向发生时,.net已经忘记了登录的用户,并且表现得好像从来没有经过过身份验证,我不知道为什么。
值得注意的是,我的身份是一个独立的解决方案,被主解决方案引用。
主要解决办法:
账户主计长:
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;
}恒等解
Startup.cs
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 -(确认DB连接字符串是正确的)
https://stackoverflow.com/a/24643382/4046026
https://stackoverflow.com/a/27500097/4046026
除了查看谷歌的一些错误教程外,任何地方似乎都认为这应该是简单明了的。我可能遗漏了一些简单的东西,或者有一个概念上的问题。
发布于 2015-07-06 14:45:11
郝公的回答是:https://stackoverflow.com/a/19102266/4046026解决了我的问题。
具体地说:
您需要在您的Startup.Auth.cs中进行以下更改:
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});https://stackoverflow.com/questions/31245581
复制相似问题