【问题标题】:OWIN Authentication not authenticatingOWIN 身份验证未进行身份验证
【发布时间】:2015-09-23 14:01:11
【问题描述】:

我是 OWIN 身份验证的新手,我正在尝试让(看似)基本的工作。

当我发布到我的登录操作“_signInManager.PasswordSignInAsync”返回成功但我的 IPrincipal 用户似乎没有更新。此外,如果我执行单独的测试(即执行 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);
        }
    }

OWIN 上下文

    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 - (确认数据库连接字符串正确)
https://stackoverflow.com/a/24643382/4046026
https://stackoverflow.com/a/27500097/4046026
以及查看一些来自谷歌的杂项教程,但似乎到处都表明这应该很简单。我可能遗漏了一些简单的东西或有概念问题。

【问题讨论】:

    标签: c# entity-framework authentication asp.net-identity owin


    【解决方案1】:

    Hao Kung 的回答:https://stackoverflow.com/a/19102266/4046026 解决了我的问题。

    具体来说:

    您需要在 Startup.Auth.cs 中进行以下更改:

    app.UseCookieAuthentication(new CookieAuthenticationOptions {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
    

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 2015-07-18
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      相关资源
      最近更新 更多