C# 如何使用Identity和Owin在ASP.NET中登录和持久化?
我正在尝试使用Identity和Owin登录用户,然后在会话结束后让他们继续登录。用户数据存储在SQL Server中。当我在调试器中运行它时,它似乎正在工作 问题是:当我将其发布到远程站点时,我可以登录,但大约5分钟后,用户不再登录。我想这是因为会话即将到期。正在使用my startup.cs中设置的正确过期时间创建cookie。我正在使用授权部分拒绝用户访问该网站。我假设我只需要用我的登录方法更改一些东西,但我似乎无法在网上找到我缺少的东西(主要是因为我不知道我在搜索什么) 这就是我登录用户的方式C# 如何使用Identity和Owin在ASP.NET中登录和持久化?,c#,asp.net,webforms,asp.net-identity,owin-middleware,C#,Asp.net,Webforms,Asp.net Identity,Owin Middleware,我正在尝试使用Identity和Owin登录用户,然后在会话结束后让他们继续登录。用户数据存储在SQL Server中。当我在调试器中运行它时,它似乎正在工作 问题是:当我将其发布到远程站点时,我可以登录,但大约5分钟后,用户不再登录。我想这是因为会话即将到期。正在使用my startup.cs中设置的正确过期时间创建cookie。我正在使用授权部分拒绝用户访问该网站。我假设我只需要用我的登录方法更改一些东西,但我似乎无法在网上找到我缺少的东西(主要是因为我不知道我在搜索什么) 这就是我登录用户
protected void SignIn(object sender, EventArgs e)
{
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(uname.Text, pwd.Text);
if (user != null)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true, AllowRefresh = true }, userIdentity);
Response.Redirect("~/Main.aspx");
}
else
{
errorlbl.Text = "Invalid username or password.";
}
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
ExpireTimeSpan = TimeSpan.FromDays(14),
CookieName = "MyCookieName",
LoginPath = new PathString("/Login.aspx/")
});
}
}