Stumped on FormsAuth Cookie Timing Out

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Stumped on FormsAuth Cookie Timing Out

    hi all, I am totally stumped, and I need your help.
    My authentication cookie (using FormsAuth against Active Directory) is
    expiring way too often (like less than 20 minutes). I have it set to expire
    in 8 hours. I'm not deploying anything to the site, so I'm not resetting the
    application during that time.

    Here's all the code which deals with any authentication. Any feedback would
    be GREATLY appreciated.

    in web.config
    <authentication mode="Forms">
    <forms loginUrl="login.aspx" name="adAuthCookie" timeout="480" path="/"
    />
    </authentication>

    User Login Function (References LDAPAuthentication class, unnecessary for
    this example)

    #region LoginUser
    private void LoginUser()
    {
    // Retrieve LDAP Connect String and Domain Name
    string sADPath =
    ConfigurationSettings.AppSettings["LDAPConnectString"].ToString();
    string sDomain =
    ConfigurationSettings.AppSettings["DomainName"].ToString();

    // Instance of LdapAuthentication class
    LDAPAuthentication oLdapAuth = new LDAPAuthentication(sADPath);

    try
    {
    if (true == oLdapAuth.IsAuthenticated(sDomain, txtUserName.Value.Trim(),
    txtPassword.Value.Trim()))
    {
    // Retrieve a list of AD Groups the User is a Member of
    string sGroups = oLdapAuth.GetGroups();

    // Create the User's FormsAuthenticationTicket
    FormsAuthenticationTicket oAuthTicket = new
    FormsAuthenticationTicket(1, txtUserName.Value.Trim(), DateTime.Now,
    DateTime.Now.AddHours(8), true, sGroups);
    // Encrypt the FormsAuthenticationTicket
    string sTicket = FormsAuthentication.Encrypt(oAuthTicket);

    // Create the auth cookie for the User
    HttpCookie oCookie = new
    HttpCookie(FormsAuthentication.FormsCookieName, sTicket);
    oCookie.Expires = DateTime.Now.AddHours(8);

    // Add the cookie to the collection
    Response.Cookies.Add(oCookie);

    // Redirect the User

    Response.Redirect(FormsAuthentication.GetRedirectU rl(txtUserName.Value.Trim(
    ), false));
    }
    else
    {
    divLoginError.Visible = true;
    lblLogin.Text = "* Sorry, you entered incorrect login credentials,
    please try again. *";
    }
    }
    catch (Exception ex)
    {
    throw (ex);
    }
    }
    #endregion

    Then in my Application_AuthenticateRequest

    #region Application_AuthenticateRequest
    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
    // Retrieve FormsAuthentication Cookie Name
    string sCookieName = FormsAuthentication.FormsCookieName;
    // Retrieve Authentication Cookie
    HttpCookie oCookie = Context.Request.Cookies[sCookieName];

    // If cookie doesn't exist, exit function
    if (null == oCookie) return;

    // Create FormsAuthenticationTicket object
    FormsAuthenticationTicket oAuthTicket = null;

    try
    {
    // Retrieve FormsAuthenticationtTicket from encrypted cookie
    oAuthTicket = FormsAuthentication.Decrypt(oCookie.Value);
    // Renew the ticket if it's expired
    if (oAuthTicket.Expired) oAuthTicket =
    FormsAuthentication.RenewTicketIfOld(oAuthTicket);
    }
    catch (Exception) { return; }

    // If FormsAuthenticationtTicket doesn't exist, exit function
    if (null == oAuthTicket) return;

    // Retrieve array of Group Names from FormsAuthenticationtTicket
    string[] sGroupsArray = oAuthTicket.UserData.Split(new char[]{'|'});

    // Create a GenericIdentity Object
    GenericIdentity oIdentity = new GenericIdentity(oAuthTicket.Name,
    "LDAPAuthentication");
    // Create a GenericPrincipal Object from the GenericIdentity and the
    Groups Array
    GenericPrincipal oPrincipal = new GenericPrincipal(oIdentity,
    sGroupsArray);

    // Assign the current HTTP instance of the application to the
    GenericPrincipal object
    Context.User = oPrincipal;

    }


    George Durzi Guest

  2. Similar Questions and Discussions

    1. Response.ReDirect / FormsAuth.ForwardFromLogin not working
      Instead of doing :- FormsAuthentication.SetAuthCookie (txtUserName.Text,false); Response.Redirect...
    2. I think no one knows the answer of timeout in formsauth!!!!!! the one i posted last week..
      -- Thanks and Regards, Amit Agarwal Software Programmer(.NET) --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system...
    3. Single signon (with FormsAuth) for mutliple web apps
      I'm working on single signon for multiple web apps on a single domain. If I authenticate in wepApp1 then I am authenticated in webApp2 however,...
    4. FormsAuth Ticket Keeps Expiring
      Calling out ASP.NET Forms Auth Experts! I need your help. Using FormsAuthentication to auth against Active Directory. During my login process,...
    5. FormsAuth and Sessions Troubles...
      I'm having some trouble implementing Forms Authentication and using Session variables... If i just turn on Forms Auth and don't set up any roles...
  3. #2

    Default Re: Stumped on FormsAuth Cookie Timing Out

    Does anyone know if there's another timeout setting that's maybe in IIS?

    I've set it in web.config, machine.config, and in my code when creating my
    cookie

    "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    news:u2TNNRtfDHA.2664@TK2MSFTNGP11.phx.gbl...
    > hi all, I am totally stumped, and I need your help.
    > My authentication cookie (using FormsAuth against Active Directory) is
    > expiring way too often (like less than 20 minutes). I have it set to
    expire
    > in 8 hours. I'm not deploying anything to the site, so I'm not resetting
    the
    > application during that time.
    >
    > Here's all the code which deals with any authentication. Any feedback
    would
    > be GREATLY appreciated.
    >
    > in web.config
    > <authentication mode="Forms">
    > <forms loginUrl="login.aspx" name="adAuthCookie" timeout="480" path="/"
    > />
    > </authentication>
    >
    > User Login Function (References LDAPAuthentication class, unnecessary for
    > this example)
    >
    > #region LoginUser
    > private void LoginUser()
    > {
    > // Retrieve LDAP Connect String and Domain Name
    > string sADPath =
    > ConfigurationSettings.AppSettings["LDAPConnectString"].ToString();
    > string sDomain =
    > ConfigurationSettings.AppSettings["DomainName"].ToString();
    >
    > // Instance of LdapAuthentication class
    > LDAPAuthentication oLdapAuth = new LDAPAuthentication(sADPath);
    >
    > try
    > {
    > if (true == oLdapAuth.IsAuthenticated(sDomain,
    txtUserName.Value.Trim(),
    > txtPassword.Value.Trim()))
    > {
    > // Retrieve a list of AD Groups the User is a Member of
    > string sGroups = oLdapAuth.GetGroups();
    >
    > // Create the User's FormsAuthenticationTicket
    > FormsAuthenticationTicket oAuthTicket = new
    > FormsAuthenticationTicket(1, txtUserName.Value.Trim(), DateTime.Now,
    > DateTime.Now.AddHours(8), true, sGroups);
    > // Encrypt the FormsAuthenticationTicket
    > string sTicket = FormsAuthentication.Encrypt(oAuthTicket);
    >
    > // Create the auth cookie for the User
    > HttpCookie oCookie = new
    > HttpCookie(FormsAuthentication.FormsCookieName, sTicket);
    > oCookie.Expires = DateTime.Now.AddHours(8);
    >
    > // Add the cookie to the collection
    > Response.Cookies.Add(oCookie);
    >
    > // Redirect the User
    >
    >
    Response.Redirect(FormsAuthentication.GetRedirectU rl(txtUserName.Value.Trim(
    > ), false));
    > }
    > else
    > {
    > divLoginError.Visible = true;
    > lblLogin.Text = "* Sorry, you entered incorrect login credentials,
    > please try again. *";
    > }
    > }
    > catch (Exception ex)
    > {
    > throw (ex);
    > }
    > }
    > #endregion
    >
    > Then in my Application_AuthenticateRequest
    >
    > #region Application_AuthenticateRequest
    > protected void Application_AuthenticateRequest(Object sender, EventArgs
    e)
    > {
    > // Retrieve FormsAuthentication Cookie Name
    > string sCookieName = FormsAuthentication.FormsCookieName;
    > // Retrieve Authentication Cookie
    > HttpCookie oCookie = Context.Request.Cookies[sCookieName];
    >
    > // If cookie doesn't exist, exit function
    > if (null == oCookie) return;
    >
    > // Create FormsAuthenticationTicket object
    > FormsAuthenticationTicket oAuthTicket = null;
    >
    > try
    > {
    > // Retrieve FormsAuthenticationtTicket from encrypted cookie
    > oAuthTicket = FormsAuthentication.Decrypt(oCookie.Value);
    > // Renew the ticket if it's expired
    > if (oAuthTicket.Expired) oAuthTicket =
    > FormsAuthentication.RenewTicketIfOld(oAuthTicket);
    > }
    > catch (Exception) { return; }
    >
    > // If FormsAuthenticationtTicket doesn't exist, exit function
    > if (null == oAuthTicket) return;
    >
    > // Retrieve array of Group Names from FormsAuthenticationtTicket
    > string[] sGroupsArray = oAuthTicket.UserData.Split(new char[]{'|'});
    >
    > // Create a GenericIdentity Object
    > GenericIdentity oIdentity = new GenericIdentity(oAuthTicket.Name,
    > "LDAPAuthentication");
    > // Create a GenericPrincipal Object from the GenericIdentity and the
    > Groups Array
    > GenericPrincipal oPrincipal = new GenericPrincipal(oIdentity,
    > sGroupsArray);
    >
    > // Assign the current HTTP instance of the application to the
    > GenericPrincipal object
    > Context.User = oPrincipal;
    >
    > }
    >
    >

    George Durzi Guest

  4. #3

    Default Re: Stumped on FormsAuth Cookie Timing Out

    I thought I'd share the solution.

    my colleague pointed out to me that there is a timeout attribute for
    sessions that's set in the web.config. It's overriding everything else. I
    had to scroll right to see it, that's why I was missing it!

    "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    news:%23o7l$vegDHA.3056@tk2msftngp13.phx.gbl...
    > Does anyone know if there's another timeout setting that's maybe in IIS?
    >
    > I've set it in web.config, machine.config, and in my code when creating my
    > cookie
    >
    > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    > news:u2TNNRtfDHA.2664@TK2MSFTNGP11.phx.gbl...
    > > hi all, I am totally stumped, and I need your help.
    > > My authentication cookie (using FormsAuth against Active Directory) is
    > > expiring way too often (like less than 20 minutes). I have it set to
    > expire
    > > in 8 hours. I'm not deploying anything to the site, so I'm not resetting
    > the
    > > application during that time.
    > >
    > > Here's all the code which deals with any authentication. Any feedback
    > would
    > > be GREATLY appreciated.
    > >
    > > in web.config
    > > <authentication mode="Forms">
    > > <forms loginUrl="login.aspx" name="adAuthCookie" timeout="480"
    path="/"
    > > />
    > > </authentication>
    > >
    > > User Login Function (References LDAPAuthentication class, unnecessary
    for
    > > this example)
    > >
    > > #region LoginUser
    > > private void LoginUser()
    > > {
    > > // Retrieve LDAP Connect String and Domain Name
    > > string sADPath =
    > > ConfigurationSettings.AppSettings["LDAPConnectString"].ToString();
    > > string sDomain =
    > > ConfigurationSettings.AppSettings["DomainName"].ToString();
    > >
    > > // Instance of LdapAuthentication class
    > > LDAPAuthentication oLdapAuth = new LDAPAuthentication(sADPath);
    > >
    > > try
    > > {
    > > if (true == oLdapAuth.IsAuthenticated(sDomain,
    > txtUserName.Value.Trim(),
    > > txtPassword.Value.Trim()))
    > > {
    > > // Retrieve a list of AD Groups the User is a Member of
    > > string sGroups = oLdapAuth.GetGroups();
    > >
    > > // Create the User's FormsAuthenticationTicket
    > > FormsAuthenticationTicket oAuthTicket = new
    > > FormsAuthenticationTicket(1, txtUserName.Value.Trim(), DateTime.Now,
    > > DateTime.Now.AddHours(8), true, sGroups);
    > > // Encrypt the FormsAuthenticationTicket
    > > string sTicket = FormsAuthentication.Encrypt(oAuthTicket);
    > >
    > > // Create the auth cookie for the User
    > > HttpCookie oCookie = new
    > > HttpCookie(FormsAuthentication.FormsCookieName, sTicket);
    > > oCookie.Expires = DateTime.Now.AddHours(8);
    > >
    > > // Add the cookie to the collection
    > > Response.Cookies.Add(oCookie);
    > >
    > > // Redirect the User
    > >
    > >
    >
    Response.Redirect(FormsAuthentication.GetRedirectU rl(txtUserName.Value.Trim(
    > > ), false));
    > > }
    > > else
    > > {
    > > divLoginError.Visible = true;
    > > lblLogin.Text = "* Sorry, you entered incorrect login credentials,
    > > please try again. *";
    > > }
    > > }
    > > catch (Exception ex)
    > > {
    > > throw (ex);
    > > }
    > > }
    > > #endregion
    > >
    > > Then in my Application_AuthenticateRequest
    > >
    > > #region Application_AuthenticateRequest
    > > protected void Application_AuthenticateRequest(Object sender,
    EventArgs
    > e)
    > > {
    > > // Retrieve FormsAuthentication Cookie Name
    > > string sCookieName = FormsAuthentication.FormsCookieName;
    > > // Retrieve Authentication Cookie
    > > HttpCookie oCookie = Context.Request.Cookies[sCookieName];
    > >
    > > // If cookie doesn't exist, exit function
    > > if (null == oCookie) return;
    > >
    > > // Create FormsAuthenticationTicket object
    > > FormsAuthenticationTicket oAuthTicket = null;
    > >
    > > try
    > > {
    > > // Retrieve FormsAuthenticationtTicket from encrypted cookie
    > > oAuthTicket = FormsAuthentication.Decrypt(oCookie.Value);
    > > // Renew the ticket if it's expired
    > > if (oAuthTicket.Expired) oAuthTicket =
    > > FormsAuthentication.RenewTicketIfOld(oAuthTicket);
    > > }
    > > catch (Exception) { return; }
    > >
    > > // If FormsAuthenticationtTicket doesn't exist, exit function
    > > if (null == oAuthTicket) return;
    > >
    > > // Retrieve array of Group Names from FormsAuthenticationtTicket
    > > string[] sGroupsArray = oAuthTicket.UserData.Split(new char[]{'|'});
    > >
    > > // Create a GenericIdentity Object
    > > GenericIdentity oIdentity = new GenericIdentity(oAuthTicket.Name,
    > > "LDAPAuthentication");
    > > // Create a GenericPrincipal Object from the GenericIdentity and the
    > > Groups Array
    > > GenericPrincipal oPrincipal = new GenericPrincipal(oIdentity,
    > > sGroupsArray);
    > >
    > > // Assign the current HTTP instance of the application to the
    > > GenericPrincipal object
    > > Context.User = oPrincipal;
    > >
    > > }
    > >
    > >
    >
    >

    George Durzi Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139