Forum Moderators: open
public class SessionHelpers
{
/// <summary>
/// Gets or Sets the AccountNumber of the Session.
/// </summary>
public static string SessionAccountNumber
{
get
{
return (string)HttpContext.Current.Session["AccountNumber"];
}
set
{
HttpContext.Current.Session["AccountNumber"] = value;
}
}
/// <summary>
/// Returns true if the user has been authenticated.
/// </summary>
public static bool isAuthenticated
{
get
{
return !String.IsNullOrEmpty(SessionAccountNumber);
}
}
}
SessionHelpers.SessionAccountNumber = "12345";
string san = SessionHelpers.SessionAccountNumber;
//and also
bool isAuthenticated = SessionHelpers.isAuthenticated;
protected void Page_Load(object sender, EventArgs e)
{
if (SessionHelpers.isAuthenticated)
{
// Show Stuff
}
else
{
// Hide Stuff
}
}
One thing I know is that some pages are going to have a pretty different layout than other pages. Would that make a difference for MasterPage?
<asp:Label ID="LogInBox" runat="server" Text="Label"></asp:Label> public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
//if the session var for account number does not exist, create it.
if (HttpContext.Current.Session["StoreAccountNumber"] == null)
{
HttpContext.Current.Session["StoreAccountNumber"] = "no";
}
//show message for login or already logged in.
if (HttpContext.Current.Session["StoreAccountNumber"] == "no")
{
LogInBox.Text = "Please visit the Login page to sign in.";
}
else
{
LogInBox.Text = "Welcome Joe Customer";
}
}
} if (IsPostBack)
{
//will check account numbers here and do stuff
HttpContext.Current.Session["StoreAccountNumber"] = LoginBox.Text;
} //if the session var for account number does not exist, create it.
if (HttpContext.Current.Session["StoreAccountNumber"] == null)
{
HttpContext.Current.Session["StoreAccountNumber"] = "no";
}
(I havent tested this code, so it may not work)
public static string SessionAccountNumber
{
get
{
return (string)HttpContext.Current.Session["AccountNumber"];
}
set
{ if (!value.StartsWith("D"))
{
Throw new Exception("Invalid Account number");
}
else
{
// Valid account number, set the session variable
HttpContext.Current.Session["AccountNumber"] = value;
}
}
public class UserAccount
{
public string AccountNumber { get; set; }
public string Name { get; set; }
public string Street { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }
}
public class SessionHelpers
{
/// <summary>
/// Gets or Sets the UserAccount of the Session.
/// </summary>
public static UserAccount UserAccount
{
get
{
return (UserAccount)HttpContext.Current.Session["UserAccount"];
}
set
{
HttpContext.Current.Session["UserAccount"] = value;
}
}
}
public class LoginHelpers
{
/// <summary>
/// Allows the user to login
/// </summary>
/// <param name="AccountNumber">The account number to get the UserAccount for.</param>
/// <returns>Returns a UserAccount object is the user has been verified, otherwise null.</returns>
public static UserAccount UserLogin(string AccountNumber)
{
// Verify the Account number (via DB or whatever)
if (//UserAccount is verified)
{
UserAccount myUserAccount = new UserAccount();
myUserAccount.AccountNumber = AccountNumber;
// Get the rest from the DB or whatever
// myUserAccount.Name = ...
// myUserAccount.Street = ...
// Also add to session
SessionHelpers.UserAccount = myUserAccount;
return myUserAccount;
}
else
{
return null;
}
}
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
string cookieName = System.Web.Security.FormsAuthentication.FormsCookieName;
if (string.IsNullOrEmpty(cookieName) == false)
{
if (this.Request.Cookies[cookieName] != null && string.IsNullOrEmpty(this.Request.Cookies[cookieName].Value) == false)
{
this.Context.User = new Ocean2.Web.Code.Principal(this.Request.Cookies[cookieName].Value);
}
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
if (this.Request.IsSecureConnection == true)
{
if (this.Context.User == null || this.Context.User.Identity.IsAuthenticated == false)
{
string strSecureURL = "http://" + this.Request.ServerVariables["SERVER_NAME"] + this.Request.Url.PathAndQuery;
this.Response.Redirect(strSecureURL);
this.Response.End();
return;
}
}
else
{
if (this.Context.User != null && this.Context.User.Identity.IsAuthenticated == true)
{
string strSecureURL = "https://" + this.Request.ServerVariables["SERVER_NAME"] + this.Request.Url.PathAndQuery;
this.Context.Response.Redirect(strSecureURL);
this.Response.End();
return;
}
}