Tuesday, January 12, 2010

301 redirect using IIS

To 301 redirect in IIS from http:// to https://,

1. Setup another site and set TCP port to 80 (original site uses other value).
2. Under "Home Directory",
Select "A redirection to a URL",
In Redirect to, type "https://www.example.com$S$Q

3. Check "The exact URL entered above" and "A Permanent redirection for this resource".

Web Design From Scratch

http://webdesignfromscratch.com/

http://office.microsoft.com/en-us/frontpage/HA011671221033.aspx

Generate a CSV from a generic list of objects using reflection and extension methods

http://www.joe-stevens.com/2009/08/03/generate-a-csv-from-a-generic-list-of-objects-using-reflection-and-extension-methods/

using System.Text;
using System.Reflection;

public static class Extensions
{
public static string GetCSV(this List list)
{
StringBuilder sb = new StringBuilder();

//Get the properties for type T for the headers
PropertyInfo[] propInfos = typeof(T).GetProperties();
for (int i = 0; i <= propInfos.Length - 1; i++)
{
sb.Append(propInfos[i].Name);

if (i < propInfos.Length - 1)
{
sb.Append(",");
}
}

sb.AppendLine();

//Loop through the collection, then the properties and add the values
for (int i = 0; i <= list.Count - 1; i++)
{
T item = list[i];
for (int j = 0; j <= propInfos.Length - 1; j++)
{
object o = item.GetType().GetProperty(propInfos[j].Name).GetValue(item, null);
if (o != null)
{
string value = o.ToString();

//Check if the value contans a comma and place it in quotes if so
if (value.Contains(","))
{
value = string.Concat("\"", value, "\"");
}

//Replace any \r or \n special characters from a new line with a space
if (value.Contains("\r"))
{
value = value.Replace("\r", " ");
}
if (value.Contains("\n"))
{
value = value.Replace("\n", " ");
}

sb.Append(value);
}

if (j < propInfos.Length - 1)
{
sb.Append(",");
}
}

sb.AppendLine();
}

return sb.ToString();
}
}

Monday, January 11, 2010

Avoid concurrent login (logout former login session) in ASP.net membership

In login.aspx:
protected void Login1_LoggedIn(object sender, EventArgs e)
{

string sKey = Login1.UserName;
string sValue = Session.SessionID;
Session[sKey] = sValue;
TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert(sKey, sValue, null, DateTime.MaxValue, SessTimeOut,
System.Web.Caching.CacheItemPriority.NotRemovable, null);

}

In Master Page of admin:
protected void Page_Load(object sender, EventArgs e)
{
string sValue = Cache[HttpContext.Current.User.Identity.Name].ToString();
if (Session.SessionID != sValue)
{
FormsAuthentication.SignOut();
Response.Redirect("login.aspx");
}
}

http://stackoverflow.com/questions/2025908/avoid-concurrent-login-logout-former-login-session-in-asp-net-membership