【发布时间】:2019-05-24 08:20:03
【问题描述】:
我在我的 api 中将此代码用于登录用户:
var ticket = new FormsAuthenticationTicket(
1,
CurrentCustommer.PhoneNumber,
DateTime.Now,
DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
false,
"user,user1",
FormsAuthentication.FormsCookiePath
);
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
{
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL,
Path = FormsAuthentication.FormsCookiePath,
Domain = FormsAuthentication.CookieDomain
};
HttpContext.Current.Response.AppendCookie(cookie);
我的 webconfig 代码是:
<authentication mode="Forms">
<forms loginUrl="Login.aspx" protection="All" timeout="10080" slidingExpiration="true">
</forms>
</authentication>
<compilation debug="true" targetFramework="4.6.2" />
<httpRuntime targetFramework="4.6.2" />
<pages enableSessionState="true" validateRequest="false"></pages>
<sessionState mode="InProc" cookieless="false" timeout="10080" />
现在。用户登录后几分钟后刷新页面或更改站点页面,自动注销;我看到通过 chrome 控制台中的“document.cookie”存储的 cookie。本地主机不存在此问题,但使用服务器时显示此问题:/
另外,我补充说我使用我的自定义数据库并且不使用 sql 成员提供程序 asp.net。
当我调用方法 API 进行用户登录时,我应该应用某些设置吗?或者我需要应用其他配置?
我真的不知道如何解决这个问题。
谢谢大家。
更新:我通过此代码检查验证用户:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.User.Identity.IsAuthenticated)
{
//Page.Response.Redirect("/");
MainContainer.Visible = false;
Page.ClientScript.RegisterStartupScript(this.GetType(),
"CallMyFunction", "LoginForm()", true);
}
}
更多信息我现在在 Page_Load 的手表中看到 Page.User.ExpireDate,这次是在用户登录后 30 分钟。
【问题讨论】:
-
你有服务器的控制权吗?如果是这样,在 iis 中设置超时,在 ASP、服务、会话属性下设置超时。
-
@Sgedda 嗨。是的,我有访问权限。碰巧几分钟后,同样的问题仍然存在。
-
它说的是什么,你的 iis 中的超时设置?
标签: asp.net api c#-4.0 web-config forms-authentication