c# - 经过几次成功的身份验证后,带有 Steam 身份验证的 IOwinContext.Authentication.AuthenticateAsync("ExternalCookie") 随机为空
问题描述
我一直在尝试使用 Owin 及其 Steam 身份验证提供程序(Owin.Security.Providers.Steam)在我的 ASP.Net Web 应用程序(ASP.Net Framework 4.8,MVC 5)中实现 Steam 身份验证。
遵循了一些关于类似身份验证系统的教程,但使用 GitHub 并重新调整了该代码以用于使用 Steam 登录。
几次登录后一切正常,但一段时间后它就会中断并且无法正确验证。
我是 Owin 的新手并使用它对用户进行身份验证,因此任何关于我应该如何调试它或我误解的与 Owin 相关的任何提示都会有所帮助。
我不知道如何解释大部分问题,我试图调试它,但没有修复它,我变得更加困惑,这是我的代码(仅相关部分):
家庭控制器.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"));
}
挑战结果.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);
}
}
}
从我在网上找到的内容来看,这应该是通用的,并且可以与任何提供商一起使用,无论是谷歌、Steam、GitHub 等,它确实......有一段时间......然后 AuthenticateAsync 每次都开始返回 null,这就是我得到的地方使困惑。我在网上找不到与此类似的问题的人,所以我猜我的代码而不是 Owin 或 IIS 配置有问题,在再次测试之前我应该检查哪些相关的 IIS 配置?
解决方案
我有同样的问题。它工作了将近两年,然后开始失败。
这个解决方案解决了我的问题:https ://coding.abel.nu/2014/11/ catch-the-system-webowin-cookie-monster/
我添加了
public void ConfigureAuth(IAppBuilder app)
{
app.UseKentorOwinCookieSaver();
作为 StartupAuth.cs 中的第一条语句,授权再次起作用。
推荐阅读
- oracle - Oracle apex 获取交互式网格行数
- javascript - 如何使用按钮滚动到表单的最后一个“文章”?
- google-bigquery - 流式插入 bigquery 是否有限制,比如每天只允许这么多插入?
- soap - 使用 Spring Web 服务针对 WSDL 验证 SOAP 消息
- http - 从 ffmpeg 流式传输到 ffserver 时出现“对等方重置连接”错误
- android - Android MVVM,Single Activity Architecture,什么是存储数据的最佳类
- iis - IIS:加载JS文件时资源失败:net :: err_http2_protocol_error
- javascript - 来自 API 的响应不是第一次在 react JS 中呈现
- javascript - 向 Google 表格提交表单时出错,导致 CORS 错误
- postgresql - Postgres - 在元数据中添加用户信息