Detect when a session has ended ASP.NET
A problem that I have had in the past is trying to find out when a session has expired within ASP.NET and how to deal with the expired session. One solution is to add the following code into the Page_Init event. You can add this event into a class that inherits the Page class so that you can reuse on more than one page or all pages if needed.
If Session.IsNewSession Then
Dim szCookieHeader As String = Request.Headers("Cookie")
If Not String.IsNullOrEmpty(szCookieHeader) And szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0 Then
Response.Redirect("/sessiontimeoutpage.aspx")
End If
End If
End If
This code checks if a new session has been created and if the header has a ASP.NET Session id. This makes sure that we are checking for a session that has just been created.
Techxplosion.net is live!
A group of us have been working on creating a new tech, geek, gadget site which includes news on the latest trends of all types of technology including mobile devices, smart phones, computers and a whole lot more! A special thanks to Andy at GT Solutions (www.ghozali.net), Seruni at Madcow Solutions and all the guys who put the effort into making this site great!
CheckoutChristchurch at The Chalice
New photo from CheckoutChristchurch!
Random number of items from a Generic List extension method
Here is a code snippet for returning N number of items from a generic list using a extension method. This requires .NET 3.5.
{
/// <summary>
/// method for returning N number of random items from a generic list
/// </summary>
/// <typeparam name="T">Item type</typeparam>
/// <param name="list">Generic list we wish to retrieve from</param>
/// <param name="count">number of items to return</param>
/// <returns></returns>
public static IEnumerable<T> Randomize<T>(this List<T> list, int count)
{
List<T> randomList = new List<T>();
Random random = new Random();
while (list.Count > 0)
{
//get the next random number between 0 and
//the list count
int idx = random.Next(0, list.Count);
//get that index
randomList.Add(list[idx]);
//remove that item so it cant be added again
list.RemoveAt(idx);
}
//return the specified number of items
return randomList.Take(count);
}
/// <summary>
/// method for returning 1 item from the generic list
/// </summary>
/// <typeparam name="T">Item type</typeparam>
/// <param name="list">Generic list we wish to retrieve from</param>
/// <param name="count">number of items to return</param>
/// <returns></returns>
public static T Randomize<T>(this List<T> list)
{
return Randomize(list, 1).FirstOrDefault();
}
}
used in code by calling the following: List<mytype> = myGenericList.Randomise(5) or mytype = myGenericList.Randomise();
Could not load file or assembly ‘NameOfAssemblyGoesHere’
Encounted the following error:
Could not load file or assembly ‘NameOfAssemblyGoesHere’ or one of its dependencies. An attempt was made to load a program with an incorrect format.
The problem happened when deploying a web application to a 64bit server where all the required DLL’s where marked as running only on a 32bit system. You can change this by setting the target platform to AnyCPU which will solve this issue. There is another workaround which is setting IIS compatability mode to 32bit, Open a command prompt and navigate to the %systemdrive%\Inetpub\AdminScripts directory. Type in the command cscript.exe adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 “true”.
What Should be in an Office First Aid Kit?
Here is some good information about what should be in a Office First Aid Kit.
Medicsafe’s Product of the week is OFFICE 1-5 PERSON FIRST AID KIT (SOFT PACK) $50.63 NZD
Spot the IT Guy!
This is soo funny but soo true?
http://www.deansproperty.com.au/Home/Profiles
Change username in ASP.NET Membership Provider
Found a great article here about the process of changing the username with the ASP.NET Membership Provider.
Here is a summary of the process that I found best useful which is one of the comments:
1. Make sure New UserName is Unique
2. Update the aspnet_Users table directly
3. Execute to following code to change the username/cookie/identity without leaving the webpage…
‘ Obtains the name of the FormsAuthentication Cookie, uses that name to request the Cookie and Decrypts the Cookies information into a AuthTicket
Dim AuthTicket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(HttpContext.Current.Request.Cookies(FormsAuthentication.FormsCookieName).Value)
‘ Instantiates a new user identity authenticated using forms authentication based on the FormsAuthenticationTicket.
‘ The FormsAuthenticationTicket has been created using the exact same parameters of the user with the Old Username except the Old Username has been updated with the New Username.
Dim NewFormsIdentity As New FormsIdentity(New FormsAuthenticationTicket(AuthTicket.Version, NewUsername, AuthTicket.IssueDate, AuthTicket.Expiration, AuthTicket.IsPersistent, AuthTicket.UserData))
‘ Parse out the AuthTicket’s UserData into a string array of Roles
Dim Roles As String() = AuthTicket.UserData.Split(“|”.ToCharArray)
‘ Creates a new user that has the NewFormsIdentity and belongs to the array of Roles, if any, that was stored in the FormsAuthenticationTicket
Dim NewGenericPrincipal As New System.Security.Principal.GenericPrincipal(NewFormsIdentity, Roles)
‘ Sets the security information for the current HTTP request to the new user. The Username has now been changed (i.e. HttpContext.Current.User.Identity.Name = NewUsername, prior to this step is was the OldUsername)
HttpContext.Current.User = NewGenericPrincipal
‘ Removes the forms-authentication ticket from the browser
FormsAuthentication.SignOut()
‘ Cancels the current session
HttpContext.Current.Session.Abandon()
‘ Creates an authentication ticket for the supplied New Username and adds it to the cookies collection of the response or the URL
FormsAuthentication.SetAuthCookie(HttpContext.Current.User.Identity.Name, AuthTicket.IsPersistent)
4. Response.Redirect back to the same page if needed.
References:
http://omaralzabir.com/how_to_change_user_name_in_asp_net_2_0_membership_provider/
IIS Wont start error 13 data is invalid
I got the following error when trying to figure out why I couldn’t start IIS 7 on Windows Server 2008 R2. I tried to start the World Wide Web Publishing Service but its dependency Windows Activation Service wouldn’t start. I got the following error in the Event Log:
The Windows Process Activation Service service terminated with the following error:
The data is invalid.
After doing some research, I found a good article here. But I still had the issue, it turned out that I had a valid applicationHost.config file but my C:\Windows\system32\inetsrv\config\schema\NetFx40_IIS_schema_update.xml file was corrupt and had invalid XML. So I was able to recover the XML file from the C:\inetput\history\schema folder. Fewl! Run all the services again and had no problems.
References:
http://www.gringod.com/2008/08/15/when-iis-wont-start-error-13/
CheckoutChristchurch.co.nz has a new pic! The Cathedral Square
CheckoutChristchurch.co.nz has a new pic! The Cathedral Square – Friday’s treat..check it out www.checkoutchristchurch.co.nz


