Tôi cũng gặp vấn đề tương tự. Trên thực tế, nó đến vì tôi không thể đọc cookie xác thực biểu mẫu từ javascript, nó được sau một thời gian undefined
. Tôi chỉ muốn biết nếu tôi đã được xác thực thông qua javascript.
Sau đó, tôi thấy rằng vé đã hết hạn, nhưng tôi đã không bị đăng xuất (cũng vì vậy tôi cũng muốn giải quyết điều đó)! Tôi thấy câu hỏi của bạn đã không được trả lời vì vậy tôi giữ nó mở trong khi tôi làm việc ra vấn đề của tôi trong nửa ngày. Sau đây là những gì tôi nghĩ ra có vẻ như đang hoạt động.
Câu trả lời của tôi dựa trên câu trả lời này. https://stackoverflow.com/a/454639/511438 bởi người dùng ScottS
Đây là dự án ASP.NET MVC 3 của tôi.
Đây là mã đăng nhập của tôi. Không được hiển thị, logic xác thực người dùng tùy chỉnh trước đó. Điều này chỉ đặt vé ban đầu.
public class FormsAuthenticationService: IFormsAuthentication
public void SignIn(string userName, bool createPersistentCookie, string role)
{
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1, // version
userName, // user name
DateTime.Now, // created
DateTime.Now.Add(FormsAuthentication.Timeout), // expires
false, // rememberMe?
role // can be used to store roles
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
HttpContext.Current.Response.Cookies.Add(authCookie);
}
trong cùng một lớp nhưng một phương pháp tĩnh được truy cập từ global.asax
//-- this is based on https://stackoverflow.com/questions/454616/asp-net-cookies-authentication-and-session-timeouts
internal static FormsAuthenticationTicket RefreshLoginCookie(bool retainCurrentExpiry)
{
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || authCookie.Value == null)
return null;
FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(authCookie.Value);
DateTime expiryDate = (retainCurrentExpiry ? oldTicket.Expiration : DateTime.Now.Add(FormsAuthentication.Timeout));
HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
var newTicket = new FormsAuthenticationTicket(oldTicket.Version, oldTicket.Name, oldTicket.IssueDate, expiryDate,
oldTicket.IsPersistent, oldTicket.UserData, oldTicket.CookiePath);
HttpCookie newAuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newTicket));
HttpContext.Current.Response.Cookies.Add(newAuthCookie);
return newTicket;
}
toàn cầu.asax
Tùy chỉnh của tôi cho rằng yêu cầu ajax không làm mới vé xác thực biểu mẫu. Vì vậy, nếu bạn ngồi ở đó trong khoảng thời gian chờ, yêu cầu ajax sẽ đăng xuất bạn. Thay đổi điều đó nếu bạn muốn yêu cầu ajax để giữ vé còn sống (giải quyết vấn đề cookie javascript của tôi, không phải vấn đề không hoạt động đăng xuất của bạn). * (mẹo, nếu bạn đăng xuất, sau đó đăng nhập, nhưng trở lại trang đăng nhập lần nữa, lần đăng nhập đầu tiên không chỉ định returnUrl trong chuỗi truy vấn có thể). *
protected virtual void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || authCookie.Value == "")
{
return;
}
bool isAjax = new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest();
FormsAuthenticationTicket authTicket;
try
{
//-- THIS IS WHAT YOU WANT
authTicket = FormsAuthenticationService.RefreshLoginCookie(isAjax);
}
catch
{
return;
}
string[] roles = authTicket.UserData.Split(';');
if (Context.User != null) Context.User = new GenericPrincipal(Context.User.Identity, roles);
}
Web.config đây là phần mà tôi đặt thời gian chờ phiên và thời gian chờ vé
<configuration>
<system.web>
<sessionState mode="InProc" timeout="60" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="60" name="ProviderMvcSession" cookieless="UseCookies" />
</authentication>
Nhưng trạng thái phiên tách biệt với cookie AuthenticTicket/.ASPXAUTH. Ít nhất tôi nghĩ nó là ?! – bplus
nếu bạn không sử dụng cookie để lưu trữ dữ liệu này, thì phiên có trách nhiệm chăm sóc nó. –
Nhưng tôi đang sử dụng cookie! Tôi đã có cookieless = "UseCookies". Cảm ơn vì đã phản hồi. Tôi thực sự bối rối bởi điều này - đề xuất của bạn hoạt động nhưng tôi không biết tại sao? – bplus