Reputation: 3103
I have a Global.asax file in my project with code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Data.SqlClient;
using WrapperObjects;
using System.Security.Principal;
namespace Application
{
public class Global : System.Web.HttpApplication
{
private string userLogin = string.Empty;
void Application_Start(object sender, EventArgs e)
{
}
void Application_End(object sender, EventArgs e)
{
}
void Application_Error(object sender, EventArgs e)
{
}
void Session_Start(object sender, EventArgs e)
{
}
void Session_End(object sender, EventArgs e)
{
DBUsers.SetUserStatusOnline("0", userLogin);
Session["curUserRole"] = string.Empty;
Session["curUserLogin"] = string.Empty;
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (app.Request.IsAuthenticated && User.Identity is FormsIdentity)
{
SqlConnection myConnection = new SqlConnection(DBConnection.GetConnectionString());
SqlCommand myCommand = new SqlCommand("SELECT * FROM Users WHERE login=@login", myConnection);
myCommand.Parameters.AddWithValue("login", app.Context.User.Identity.Name);
myCommand.Connection.Open();
SqlDataReader Reader = myCommand.ExecuteReader();
string role = string.Empty;
while (Reader.Read())
{
userLogin = Reader["login"].ToString();
role = Reader["role"].ToString();
}
if (role != string.Empty)
{
FormsIdentity fi = (FormsIdentity)app.User.Identity;
app.Context.User = new GenericPrincipal(fi, new string[] { role });
}
}
}
}
}
When user login into the project run function protected void Application_AuthenticateRequest(object sender, EventArgs e)
and I try to store user login in private string type userLogin
variable. But when session end run function void Session_End(object sender, EventArgs e)
in this function I try to save in database by user login status "offline" but variable userLogin
is empty. Where I can quickly save user login and get it in function void Session_End(object sender, EventArgs e)
?
Upvotes: 1
Views: 1791
Reputation: 11964
Because of stateless of web applications you can't do this through static variable. If I where you, I would save login to Session state in
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (app.Request.IsAuthenticated && User.Identity is FormsIdentity)
{
SqlConnection myConnection = new SqlConnection(DBConnection.GetConnectionString());
SqlCommand myCommand = new SqlCommand("SELECT * FROM Users WHERE login=@login", myConnection);
myCommand.Parameters.AddWithValue("login", app.Context.User.Identity.Name);
myCommand.Connection.Open();
SqlDataReader Reader = myCommand.ExecuteReader();
var userLogin = string.Empty;
string role = string.Empty;
while (Reader.Read())
{
userLogin = Reader["login"].ToString();
role = Reader["role"].ToString();
}
if (role != string.Empty)
{
FormsIdentity fi = (FormsIdentity)app.User.Identity;
app.Context.User = new GenericPrincipal(fi, new string[] { role });
Session["UserLogin"] = userLogin;
}
}
}
void Session_End(object sender, EventArgs e)
{
if(Session["UserLogin"]!=null)
{
var userLogin = (string)Session["UserLogin"];
DBUsers.SetUserStatusOnline("0", userLogin);
}
}
Upvotes: 1