Application_AuthenticateRequest cannot read Session variable

G

Guest

I will be using a companyname, user name, and password to authenicate users
in my system. I am trying to save the company name in the session for later
use. I cannot access the Session["CompanyName"] object in the
Application_AuthenticateRequest function. I need the companyname to lookup
the users group to build the GenericPrincipal for the user. My code is below.

Login Page:
int c = SiteSecurity.DBAuthenticate(txtCompanyName.Text, txtUsername.Text,
txtPassword.Text);
if (c > 0) {
//save the company name into the session
Session.Add("CompanyName", txtCompanyName.Text);
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);
}
else {
//error handling
}

GLobal.asax
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if ( Context.Request.IsAuthenticated ) {
string strUserName;
String strRoles;
String userName = String.Empty;

strUserName = Context.User.Identity.Name;
strRoles = EMS.PepSecurity.GetClientUsersGroups(strUserName);

string[] arrRoles = strRoles.Split('|');
Context.User = new GenericPrincipal( Context.User.Identity, arrRoles );
}
}

In the EMS.PepSecurity.GetClientUsersGroups function I am getting the error
on the following lines
string companyName = string.Empty;
try { companyName = HttpContext.Current.Session["CompanyName"].ToString(); }

The object cannot be found.

How can I save the company name if the session will not work?
 
G

Guest

Session is just that - only relevent for a specific users session, sounds
like you need to use the "Application" object to store this on, similar, just
different scope.

Dave
 
W

William F. Robertson, Jr.

Session is not yet bound to the context during Authenticate. You will need
to place this information in a cookie to persist it correctly.

bill


danman226 said:
I will be using a companyname, user name, and password to authenicate users
in my system. I am trying to save the company name in the session for later
use. I cannot access the Session["CompanyName"] object in the
Application_AuthenticateRequest function. I need the companyname to lookup
the users group to build the GenericPrincipal for the user. My code is below.

Login Page:
int c = SiteSecurity.DBAuthenticate(txtCompanyName.Text, txtUsername.Text,
txtPassword.Text);
if (c > 0) {
//save the company name into the session
Session.Add("CompanyName", txtCompanyName.Text);
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);
}
else {
//error handling
}

GLobal.asax
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if ( Context.Request.IsAuthenticated ) {
string strUserName;
String strRoles;
String userName = String.Empty;

strUserName = Context.User.Identity.Name;
strRoles = EMS.PepSecurity.GetClientUsersGroups(strUserName);

string[] arrRoles = strRoles.Split('|');
Context.User = new GenericPrincipal( Context.User.Identity, arrRoles );
}
}

In the EMS.PepSecurity.GetClientUsersGroups function I am getting the error
on the following lines
string companyName = string.Empty;
try { companyName =
HttpContext.Current.Session["CompanyName"].ToString(); }
 
G

Guest

Thanks.

I rewrote my forms authentication request to get it to work.

William F. Robertson said:
Session is not yet bound to the context during Authenticate. You will need
to place this information in a cookie to persist it correctly.

bill


danman226 said:
I will be using a companyname, user name, and password to authenicate users
in my system. I am trying to save the company name in the session for later
use. I cannot access the Session["CompanyName"] object in the
Application_AuthenticateRequest function. I need the companyname to lookup
the users group to build the GenericPrincipal for the user. My code is below.

Login Page:
int c = SiteSecurity.DBAuthenticate(txtCompanyName.Text, txtUsername.Text,
txtPassword.Text);
if (c > 0) {
//save the company name into the session
Session.Add("CompanyName", txtCompanyName.Text);
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);
}
else {
//error handling
}

GLobal.asax
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if ( Context.Request.IsAuthenticated ) {
string strUserName;
String strRoles;
String userName = String.Empty;

strUserName = Context.User.Identity.Name;
strRoles = EMS.PepSecurity.GetClientUsersGroups(strUserName);

string[] arrRoles = strRoles.Split('|');
Context.User = new GenericPrincipal( Context.User.Identity, arrRoles );
}
}

In the EMS.PepSecurity.GetClientUsersGroups function I am getting the error
on the following lines
string companyName = string.Empty;
try { companyName =
HttpContext.Current.Session["CompanyName"].ToString(); }
The object cannot be found.

How can I save the company name if the session will not work?
 
B

Brock Allen

The way to solve this is to cache the user's role mappings in the ASP.NET
data Cache object. You can generate a key based upon the username and fetch
them that way. So, the Session isn't needed to cache that data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top