Dear All,
I was given a task to validate that the user has logged in once at a time, i.e the user once logged in cannot login from same or other place until logs out.
Below is the code for the events on my Login Page:
//create object of global.asax class
Global obj = new Global();
protected void Login_Click(object sender, EventArgs e)
{
bool result=obj.GenerateSession(TextBox1.Text,true);
if(result){
Label1.Text = "Logged in Sucessfully";
}
else
{
Label1.Text = "You are already logged in";
}
}
protected void Logout_Click(object sender, EventArgs e)
{
bool result=obj.DeleteValue(TextBox1.Text);
if (result)
{
Label1.Text = "Logged Out Sucessfully";
}
else
{
Label1.Text = "You are already logged Out";
}
}
Below is the code of Global.asax file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Net;
namespace Test
{
public class Global : HttpApplication
{
static Dictionary<string, bool> SessionDict = new Dictionary<string, bool>();
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterOpenAuth();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
void Session_OnEnd(object sender, EventArgs e)
{
}
public bool GenerateSession(string bname,bool val)
{
bool _value = GetValue(bname);
if (_value)
{
//not allowed to login as session already exists
return false;
}
else
{
//loggingin for the first time
SetValue(bname, val);
return (GetValue(bname));
}
}
public void SetValue(string key, bool value)
{
SessionDict.Add(key, value);
}
public bool DeleteValue(string _str)
{
bool value = false;
if (SessionDict.ContainsKey(_str))
{
if (SessionDict[_str] != null)
{
value = SessionDict[_str];
value=SessionDict.Remove(_str);
return value;
}
else
{
return false;
}
}
else
{
return false;
}
}
//Get value from cokkie
public bool GetValue(string _str)
{
bool value = false;
if (SessionDict.ContainsKey(_str))
{
if (SessionDict[_str] != null)
{
value = SessionDict[_str];
return value;
}
else
{
return false;
}
}
else
{
return false;
}
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
}
}
I was given a task to validate that the user has logged in once at a time, i.e the user once logged in cannot login from same or other place until logs out.
Below is the code for the events on my Login Page:
//create object of global.asax class
Global obj = new Global();
protected void Login_Click(object sender, EventArgs e)
{
bool result=obj.GenerateSession(TextBox1.Text,true);
if(result){
Label1.Text = "Logged in Sucessfully";
}
else
{
Label1.Text = "You are already logged in";
}
}
protected void Logout_Click(object sender, EventArgs e)
{
bool result=obj.DeleteValue(TextBox1.Text);
if (result)
{
Label1.Text = "Logged Out Sucessfully";
}
else
{
Label1.Text = "You are already logged Out";
}
}
Below is the code of Global.asax file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Net;
namespace Test
{
public class Global : HttpApplication
{
static Dictionary<string, bool> SessionDict = new Dictionary<string, bool>();
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterOpenAuth();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
void Session_OnEnd(object sender, EventArgs e)
{
}
public bool GenerateSession(string bname,bool val)
{
bool _value = GetValue(bname);
if (_value)
{
//not allowed to login as session already exists
return false;
}
else
{
//loggingin for the first time
SetValue(bname, val);
return (GetValue(bname));
}
}
public void SetValue(string key, bool value)
{
SessionDict.Add(key, value);
}
public bool DeleteValue(string _str)
{
bool value = false;
if (SessionDict.ContainsKey(_str))
{
if (SessionDict[_str] != null)
{
value = SessionDict[_str];
value=SessionDict.Remove(_str);
return value;
}
else
{
return false;
}
}
else
{
return false;
}
}
//Get value from cokkie
public bool GetValue(string _str)
{
bool value = false;
if (SessionDict.ContainsKey(_str))
{
if (SessionDict[_str] != null)
{
value = SessionDict[_str];
return value;
}
else
{
return false;
}
}
else
{
return false;
}
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
}
}
1 comment:
I feel there is a need to look for more REST API utilities and hence there can be more utilities of this.
SQL Server Load Rest API
Post a Comment
Comments are welcome, Please join me on my Linked In account
http://in.linkedin.com/pub/ritesh-tandon/21/644/33b