经过几次成功的身份验证后,带有Steam身份验证的IOwinContext.Authentication.AuthenticateAsync(“ ExternalCookie”)随机为空

时间:2020-07-07 10:28:12

标签: c# asp.net asp.net-mvc owin steam

我一直在尝试使用Owin及其Steam身份验证提供程序(Owin.Security.Providers.Steam)在我的ASP.Net Web应用程序(ASP.Net Framework 4.8,MVC 5)中实现Steam身份验证。

遵循了一些有关类似身份验证系统的教程,但使用了GitHub,并重新修改了该代码以用于Steam登录。

通过几次登录,一切都可以正常工作,但是一段时间后,它会中断并且无法正确进行身份验证。

我是Owin的新用户,正在使用它进行用户身份验证,因此有关调试该工具的提示或我误解的与Owin相关的任何信息都将有所帮助。

我不知道如何解释很多问题,我正在尝试调试它,但我没有解决它,只是更加困惑,这是我的代码(仅相关部分):

HomeController.cs

public async Task<ActionResult> Login()
{
    // This is always null after a couple of succuessful authentications
    var authenticateResult = await HttpContext.GetOwinContext().Authentication.AuthenticateAsync("ExternalCookie");

    if(authenticateResult != null)
    {
        var firstOrDefault = authenticateResult.Identity.Claims.FirstOrDefault(claim => claim.Issuer == "Steam" && claim.Type.Contains("nameidentifier"));

        var idString = firstOrDefault?.Value;
        var match = _accountIdRegex.Match(idString ?? "");

        if (match.Success)
        {
            var accountID = match.Groups[1].Value;
            var steamID = ulong.Parse(accountID);

            // User Management Code

            return RedirectToAction("Index");
        }
    }

    return RedirectToAction("LoginSteam");
}

public ActionResult LoginSteam()
{
    return new ChallengeResult("Steam", Url.Action("Login"));
}

ChallengeResult.cs

internal class ChallengeResult : HttpUnauthorizedResult
{
    public ChallengeResult(string provider, string redirectUri)
    {
        LoginProvider = provider;
        RedirectUri = redirectUri;
    }

    public string LoginProvider { get; set; }
    public string RedirectUri { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
        context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
    }
}

和Startup.cs

[assembly: OwinStartup(typeof(ApplicationNamespace.Startup))]
namespace ApplicationNamespace
{
    public class Startup
    {
        public static string steamKey = "";

        public void Configuration(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType("ExternalCookie");

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "ExternalCookie",
                AuthenticationMode = AuthenticationMode.Passive,
                CookieName = ".AspNet.ExternalCookie",
                ExpireTimeSpan = TimeSpan.FromMinutes(5)
            });

            var webconfig = WebConfigurationManager.OpenWebConfiguration("~/");
#if DEBUG
            steamKey = webconfig.AppSettings.Settings["steamApiKey"].Value;
#else
            steamKey = webconfig.AppSettings.Settings["steamApiKeyRelease"].Value;
#endif

            var options = new SteamAuthenticationOptions
            {
                ApplicationKey = steamKey,
            };

            app.UseSteamAuthentication(options);
        }
    }
}

根据我在网上找到的内容,它应该是通用的,并且可以与任何提供商(无论是Google,Steam,GitHub还是其他提供商)一起使用,并且确实存在……一段时间……然后AuthenticateAsync每次都开始返回null,即我感到困惑。 我找不到任何与此在线有类似问题的人,所以我想我的代码而不是Owin或IIS配置有问题,在再次测试之前应该检查哪些相关的IIS配置?

首先感谢您尝试提供帮助的人,这非常重要!

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。它工作了将近两年,然后开始失败。

我的解决方案是这样解决的:https://coding.abel.nu/2014/11/catching-the-system-webowin-cookie-monster/

我添加了

public void ConfigureAuth(IAppBuilder app)
    {
      app.UseKentorOwinCookieSaver();

作为 StartupAuth.cs 中的第一条语句,授权再次生效。

相关问题