Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET身份验证会话和cookie仅在本地工作_C#_Asp.net Mvc_Authentication_Asp.net Identity - Fatal编程技术网

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

C# ASP.NET身份验证会话和cookie仅在本地工作,c#,asp.net-mvc,authentication,asp.net-identity,C#,Asp.net Mvc,Authentication,Asp.net Identity,我使用的是ASP.NET Identity,它在本地与常规登录和外部登录完美结合。由于某些原因,当我发布我的项目并在远程服务器上运行它时,我有大约1分钟的授权会话。1分钟后,我被重定向到我的登录页面。(无错误消息) 我的启动验证配置: public partial class Startup { public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(Ap

我使用的是ASP.NET Identity,它在本地与常规登录和外部登录完美结合。由于某些原因,当我发布我的项目并在远程服务器上运行它时,我有大约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);
}
公共部分类启动
{
public void ConfigureAuth(IAppBuilder应用程序)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);

app.CreatePerOwinContext"此问题最常见的原因之一是没有在web.config中设置
MachineKey
元素。
MachineKey
用于加密和解密授权cookie等内容。
MachineKey
元素未设置时,IIS将补足为您准备一台
MachineKey

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


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

猜测:任何
角色
都会限制对资源的访问(和重定向)?是的。我使用用户以管理员角色登录,并且在仅限于管理员角色的页面上工作。本地工作正常,但在服务器上,我会在1分钟后重定向到登录页面。有多少台服务器?如果>1并且在负载平衡器后面,您可能会遇到会话问题。您是否在web.config中设置了机器密钥?这是.NET用来加密/解密aut的h cookie等。如果未设置,并且您使用的是某种类型的共享主机(如Azure),不同的web服务器将无法读取cookie。通过设置机器密钥,您可以确保应用程序的任何实例都能够读取执行授权所需的相关cookie。Tommy,非常感谢!不知道我怎么会错过它!请将您的评论作为我问题的答案发布,以便我可以将其标记为发誓!
[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;
            }
  }