I am doing the following during login, but the logins don t seem to be persisting at all:
FormsAuthentication.SetAuthCookie(userId.ToString(), true);
I am doing the following during login, but the logins don t seem to be persisting at all:
FormsAuthentication.SetAuthCookie(userId.ToString(), true);
You have run into a bug that MS calls an undocumented security feature.
In order to set a persistent cookie you need to create it yourself and set the Expiration explicitly. The only trick is to get the FormsAuthentication timeout value, which, in their infinite wisdom, microsoft has not exposed since 1.0. I have provided my method for getting this value.
Here is a working example.
Login.aspx
<%@ Page Language="C#" %>
<script runat="server">
protected void Login1_LoggedIn(object sender, EventArgs e)
{
var login = (Login)sender ;
if (login.RememberMeSet)
{
// hack to get forms timeout - it is not publicly surfaced anywhere.
var tmpTicket = FormsAuthentication.GetAuthCookie("foo", true);
var timeout = tmpTicket.Expires;
// create a new ticket
FormsAuthenticationTicket ticket =
new FormsAuthenticationTicket(2, login.UserName, DateTime.Now, timeout, true, "", FormsAuthentication.FormsCookiePath);
string ticketEncrypted = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted)
{
HttpOnly = true,
Path = FormsAuthentication.FormsCookiePath,
Secure = FormsAuthentication.RequireSSL,
Expires = ticket.Expiration
};
Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
Response.Cookies.Add(cookie);
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" OnLoggedIn="Login1_LoggedIn">
</asp:Login>
</div>
</form>
</body>
</html>
Does anyone know of a way to get ASP.NET Forms Authentication to not redirect back to the login page if a user is not allowed to visit a certain page or folder based on their role (and perhaps show a ...
I need to use Form-Based authentication on an ASP.NET MVC web site with LDAP (Active Directory) backend like TeamCity for instance. So i need to query LDAP first if the requested user is valid, then ...
I m using formsAuthentication and as an admin user to the site i would like to be able to sign another user out. Is this possible ?
I m using ASP.Net and forms authentication. When a user is directed to the Login Page I get a JavaScript error: Message: Syntax error Line: 3 Char: 1 Code: 0 URI: http://localhost:49791/login....
Our application is built in VB6 and delivered in a SaaS model via Citrix. Our subscribers must first authenticate to the Citrix Login Portal (AD) which gives them access to their applications. Each ...
I am doing the following during login, but the logins don t seem to be persisting at all: FormsAuthentication.SetAuthCookie(userId.ToString(), true);
I have a user database, to which I have access trough a web service. One of the web service method is something like this: public void login(string name, string password, out user_key) and in my ...
I have a ASP.Net application in my login page I call FormsAuthentication.SignOut Session.Abandon() Session.Clear() however the Appscan is taking the ASPXAUTH cookie value then after logout is ...