ASP.NET身份验证会话和cookie仅在本地工作

时间:2018-04-14 14:22:07

标签: c# asp.net-mvc authentication asp.net-identity

我使用的是ASP.NET身份,它可以通过常规登录和外部登录在本地完美运行。出于某种原因,当我发布我的项目并在远程服务器上运行它时,我的授权会话大约有1分钟。 1分钟后,我被重定向到我的登录页面。 (没有错误信息)

我的启动验证配置:

public partial class Startup
    {
     public void ConfigureAuth(IAppBuilder app)
        {
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                ExpireTimeSpan = TimeSpan.FromDays(2),
                Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, VisU>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });
            var googleOptions = new GoogleOAuth2AuthenticationOptions()
                   {
                       ClientId = "***",
                       ClientSecret = "***",
                       SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
                       Provider = new GoogleOAuth2AuthenticationProvider()
                       {
                           OnAuthenticated = (context) =>
                       {
                           context.Identity.AddClaim(new Claim("urn:google:name", context.Identity.FindFirstValue(ClaimTypes.Name)));
                           context.Identity.AddClaim(new Claim("urn:google:email", context.Identity.FindFirstValue(ClaimTypes.Email)));
                           context.Identity.AddClaim(new Claim("urn:google:accesstoken", context.AccessToken, ClaimValueTypes.String, "Google"));
                           return Task.FromResult(0);
                       }
                   }
               };
               app.UseGoogleAuthentication(googleOptions);
}

我的帐户管理员:

[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;
    return View();
}


[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<string> Login()
{
            var result = await SignInManager.PasswordSignInAsync(Request.Form["emailLogin"], Request.Form["passwordLogin"], true, shouldLockout: true);
            switch (result)
            {
                case SignInStatus.Success:
                    return ViewBag.ReturnUrl ?? "https://localhost:44300/Account";
                case SignInStatus.LockedOut:
                    return Resources.Multilang.ERRORLockedOut;
                case SignInStatus.Failure:
                    //Mail or password are incorrect
                    return Resources.Multilang.ERRORInvalidLogin;
                default:
                    return Resources.Multilang.ERRORInvalidLogin;
            }
  }

这种行为可能是什么原因?

(&#34; https://localhost:44300/&#34;在发布时更改为我的域名。)

1 个答案:

答案 0 :(得分:1)

此问题的最常见原因之一是未在web.config中设置MachineKey元素。 MachineKey用于加密和解密诸如授权cookie之类的内容。如果未设置MachineKey元素,IIS将为您组成MachineKey

如果这是一个问题,如果您将应用程序发布到第三方Web托管,例如Azure或GoDaddy。这些提供程序通常在多个Web服务器上具有Web应用程序。由于Web服务器将自己构建MachineKey(如果未设置),因此托管应用程序的每个Web服务器都将拥有自己的MachineKey。最终结果是Web服务器A发布并加密授权cookie。如果下一个请求进入Web服务器B,它将无法解密和读取cookie,因此它假定您没有登录。

在web.config中设置MachineKey可确保托管应用程序的每台Web服务器都可以加密和解密授权cookie而不会出现问题。

相关问题