Asp.net mvc 请记住我的登录不工作在服务器上,但在本地工作非常好
如果您现在登录以记住我,并且在5分钟内什么也不做,您将需要再次登录 但是,如果我在本地运行我的web应用程序,它工作得非常好 我尝试调试我的应用程序,并且记住我已为真并成功登录,并且仅在本地持久登录效果很好,但当发布我的应用程序并将其上载到主机时,记住我=真将不会持久 我的代码是诈骗个人身份验证 我的登录功能Asp.net mvc 请记住我的登录不工作在服务器上,但在本地工作非常好,asp.net-mvc,asp.net-mvc-5,Asp.net Mvc,Asp.net Mvc 5,如果您现在登录以记住我,并且在5分钟内什么也不做,您将需要再次登录 但是,如果我在本地运行我的web应用程序,它工作得非常好 我尝试调试我的应用程序,并且记住我已为真并成功登录,并且仅在本地持久登录效果很好,但当发布我的应用程序并将其上载到主机时,记住我=真将不会持久 我的代码是诈骗个人身份验证 我的登录功能 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) {
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var user = context.Users.FirstOrDefault(x => x.Email == model.Email || x.UserName == model.Email);
if (user == null)
{
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
if (!await UserManager.IsEmailConfirmedAsync(user.Id))
{
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id,
"Welcome to lotusoi", "Hi ,Confirm your email address to help us ensure your account is always protected in lotusoi.at<br><a style='border:none;color:#fff;background:#665cac;width:105px;padding:10px;display:block;font-size:16px;border-radius:360px;text-decoration:none' href=\"" + callbackUrl + "\">Confirm Now</a>");
TempData["userId"] = user.Id;
return RedirectToAction("EmailConfirmation");
}
if (await UserManager.IsLockedOutAsync(user.Id))
{
return View("Lockout");
}
var result = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
await SignInManager.SignInAsync(user,model.RememberMe,model.RememberMe);
if (model.RememberMe)
{
var cookie = FormsAuthentication.GetAuthCookie(user.UserName, model.RememberMe);
var d = cookie.Expires;
cookie.Expires = DateTime.Now.AddYears(2);
Response.Cookies.Add(cookie);
}
else
{
FormsAuthentication.SetAuthCookie(user.UserName, model.RememberMe);
}
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
默认设置为my Startup.Auth.cs
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
public void ConfigureAuth(IAppBuilder应用程序)
{
//将数据库上下文、用户管理器和登录管理器配置为每个请求使用一个实例
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext(ApplicationUserManager.Create);
app.CreatePerOwinContext(ApplicationSignInManager.Create);
//使应用程序能够使用cookie存储登录用户的信息
//以及使用cookie临时存储用户登录第三方登录提供商的信息
//配置登录cookie
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
//允许应用程序在用户登录时验证安全戳。
//这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
//允许应用程序在验证双因素身份验证过程中的第二个因素时临时存储用户信息。
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie,TimeSpan.FromMinutes(5));
//使应用程序能够记住第二个登录验证因素,如电话或电子邮件。
//选中此选项后,登录过程中的第二步验证将在您登录的设备上被记住。
//这类似于登录时的RememberMe选项。
app.useTowFactoryMemberBrowserCookie(DefaultAuthenticationTypes.TwoFactoryRememberBrowserCookie);
}
您的IIS应用程序池可能由于不活动而超时。默认情况下,应用程序池在一段时间后终止,这样做会终止您的会话,因此即使您启用了持久登录,并且用户有一个长时间的身份验证cookie来登录,如果应用程序池由于不活动而关闭,他们也可能需要再次登录。您可以通过执行以下操作在IIS设置中更改此设置:
- 转到IIS管理器中的
选项卡应用程序池
- 右键单击当前使用的应用程序池,然后选择“高级设置”
- 将
设置为更高的值或空闲超时(分钟)
使其永不终止0
- 重新启动应用程序池
非常感谢您引起我的注意,这是IIS for server中的idel问题,而不是我的代码我试图更改此空闲超时为server,但我无法更改,因此我现在与支持此服务器的人员交谈
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}