Sunday, October 30, 2011

How to check SessionTimeout in ASP.NET

The following code is useful to check Session Timeout.

protected bool SessionTimeout()

{

bool timeOut = false;

if (Context.Session != null)

{

if (Context.Session.IsNewSession)

{

string sCookieHeader = Page.Request.Headers["Cookie"];

if ((null != sCookieHeader) && (sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))

{

timeOut = true;

}

}

}

return timeOut;


How to convert string to Base64String, vice versa

You want to change stirng to base64string, vice versa, the following code is useful.

public static string ToBase64String(string s)

{

byte[] aby = UTF8Encoding.UTF8.GetBytes(s);

return Convert.ToBase64String(aby);

}

public static string FromBase64String(string s)

{

byte[] aby = Convert.FromBase64String(s);

return UTF8Encoding.UTF8.GetString(aby);

}


How to fix missing session in iframe using ASP.NET

When you face missing session in iframe, it can solve with the following code:

Response.AppendHeader("P3P", "CP=\"CAO PSA OUR\"");

You put the above code in Page Load event. That's it.

How to check the string whether integer or not?

When you want to check strings as integer, you can use the following code easily:

private
bool checkDigit(string s)

{

System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("^([0-9]+)$");

return (re.Matches(s).Count > 0);

}

How to get web site url in asp.net


When you want to get one web site url such as http://www.iblogseeker.com/register.aspx, use the following code:
 
public string GetSiteUrl()
{
string baseUrl = null;
HttpContext c = HttpContext.Current;
if (c != null)
{
string port = c.Request.ServerVariables["SERVER_PORT"];
if (port == null || port.Equals("80") || port.Equals("443"))
port = String.Empty;
else
port = ":" + port;
string protocol = c.Request.ServerVariables["SERVER_PORT_SECURE"];
if (protocol == null || protocol.Equals("0"))
protocol = "http://";
else
protocol = "https://";
baseUrl = protocol + c.Request.ServerVariables["SERVER_NAME"] + port + c.Request.ApplicationPath;
}
return baseUrl;
}

It covers any url in active on web.