środa, 23 września 2009

ASP.NET MVC "Remember me" and FormsAuthentication timeout

Recently I came across a strange behavior of ASP FormsAuthentication class. As it is said in "Pro ASP.NET 3.5 in C# 2008" book forms authentication should create persistent cookie when user marks "Remember me" checkbox in login control. Persistent cookie should avoid logging off user when he closes browser or when default timeout passes (it is configured in forms authentication section in Web.Config file).
Listing 1: Web.Config - Forms authentication configuration
  
         
  

To avoid logging off user even if default timeout goes by I needed to edit SignIn method from FormsAuthenticationService class which is placed in AccountController.cs file.
Listing 2: Updated SignIn method
public void SignIn(string userName, bool createPersistentCookie)
{
    // Remember me was checked - set cookie to remember user for 10 days (or until he logs off)
    if (createPersistentCookie)
    {
        var tenDaysFromNow = DateTime.Now.AddDays(10);
        FormsAuthentication.Initialize();
        HttpCookie cookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie);
        cookie.Expires = tenDaysFromNow;
        var cookieVal = FormsAuthentication.Decrypt(cookie.Value);
        FormsAuthenticationTicket at = new FormsAuthenticationTicket(cookieVal.Version, cookieVal.Name, cookieVal.IssueDate, tenDaysFromNow, true, cookieVal.UserData);
        cookie.Value = FormsAuthentication.Encrypt(at);
        HttpContext.Current.Response.Cookies.Add(cookie);                                              
    }            
    else
    {
        FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);                 
    }            
}

The code grabs default authentication cookie (line 8), decrypts its value in line 10 and based on existing value creates new Authentication Ticket with updated ExpirationDate. In the end cookie has been added to response cookies collection.

piątek, 18 września 2009

poniedziałek, 14 września 2009

Regular Expressions in C#

This post is going to explain or memorize my thoughts about regular expressions. I don't use it very often, so that every time I come back to this topic I have to remind myself some of details and that's why I decided to publish this post. The library where you can find a lot of examples is here: http://regexlib.com/ There is also an good freeware program to test your expressions: http://www.radsoftware.com.au/regexdesigner/ The Regex class is placed in
using System.Text.RegularExpressions;
namespace. Our input string looks like this: Jacek Skowron **** Mielec 1984 Address line description description description description description description description description description description description description description description description description description description description description Krystian Kapel *** Szczucin 1983 Addres line description description description description description description description description description description description description description description description description description description description description This string contains set of people. Each person is described like that: First line: Name and rating. Second line: birth place and year, third line contains address and the last line description. My regex expresssion used to parse details from input string is:
   1:  (?<name>.+?)\s(?<rating>\*+).*?\n
   2:  (?<birthPlace>.+?)(?<year>\d{4}?).*?\n
   3:  (?<address>.+?)\n
   4:  (?<description>.+?)\n
Let examine this expression step by step starting from simple versions and exploring it.
First line: It could look like this:
   1:  .+?\s\*+.*?\n
Let's assume there is even no question marks. Explanation then is simple: '.' firstly dot sign stands for any character (except new line \n sing). '+' sign specifies that we want to find one or more characters (here any characters because of dot), '\s' stands for single white-space. '\*+' Next there is an escaped start sign combined with + sign responsible again for finding one or more occurrences of stars. '.*\n' All is left in this line is an hard enter sign - '\n', but I inserted before it '.*' and it is going to find any white-spaces between last star and hard enter. Adding question marks prevents Regular expressions from being greedy, meaning they match as much as they can. (for example it could find name in starting line and stars (rating) in last line of our input string. Having said, first question mark ensures that regex will find all text until first occurance of space fallowed by stars and second ? tells regex to find first hard return. We're almost done with first line, except of brackets, which allow us to divide expression into subexpressions (groups).
   1:  (?<name>.+?)
Referencing to our first line and finding person's name I surrounded expression responsible for finding name with brackets, i.e (?expression) and then added group name in angle bracket. The same was done for rating expression:
(?<rating>\*+)

Second line:
   2:  (?<birthPlace>.+?)(?<year>\d{4}?).*?\n
It introduces only one new aspect - finding digits (specified by '\d') and here where we want to find year (always 4 digits) '\d{4}'
Third and fourth line is just an subset of first line where we were looking for person's name. Regular expression is done. Let's use it in code: Listing - using our expression.
Regex rgx = new Regex(@"(?<name>.+?)\s(?<rating>\*+).*?\n(?<birthPlace>.+?)(?<birthYear>\d{4}?).*?\n(?<address>.+?)\n(?<description>.+?)\n",
 RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
                    MatchCollection m = rgx.Matches(inputstring);

                    foreach (Match item in m)
                    {
                        Person p = new Person ();
                        p.Name= item.Groups["name"].Value;
                        p.Rating= item.Groups["rating"].Value.Length;
                        p.BirthPlace = item.Groups["birthPlace"].Value.Trim();
                        p.BirthYear= item.Groups["birthYear"].Value.Trim();
                        p.Address = item.Groups["address"].Value.Trim();
                        p.Description = item.Groups["description"].Value.Trim();
                        // Process our person object
                    }
Variable inputstring contains text with our string to parse. MatchCollection contains all people parsed by regex. Because we've named groups earlier looping over regex result and finding person's properties is easy and straighforward.

piątek, 4 września 2009

Create Fixed div where you can add messages that will fade out automatically.

When you use a lot of ajax requests in your site, especially requests that make some changes on server side and do not return any html content, there is a need to inform user if the request he performed has succeeded. The output will look like this: The plan to achieve it is: 1) Create a hidden div with fixed position in your MasterPage file: 2) Create javaScript function that will show your text for some time. 3) Call this function when your ajax request succeeded. ad. 1) Listing 1: Div html
   1:  <div id="ajax_messages" style="">Operation succeeded</div>
Listing 2: Div style
   1:  #ajax_messages {
   2:  background-color:#FFFF99;
   3:  bottom:0;
   4:  display:block;
   5:  font-family:Arial,Helvetica,sans-serif;
   6:  font-size:110%;
   7:  font-weight:bold;
   8:  height:20px;
   9:  margin:0;
  10:  padding:5px 0 0;
  11:  position:fixed;
  12:  text-align:center;
  13:  width:100%;
  14:  }
ad. 2) In your MasterPage file place javascript source code. This code uses jQuery js library. Listing 3: showMessage function
   1:  <script type="text/javascript">        
   2:          function showMessage(text) {
   3:              var am = $("#ajax_messages");
   4:              am.html(text);
   5:              am.show();
   6:              am.fadeOut(900);
   7:          }
   8:  </script>
The 3rd line grabs the div, 4th places your text in it, 5th shows the div and 6th makes the div disappear slowly (900 is the time of fading out in milliseconds). ad. 3) Listing 4: An example of using this function
   1:  <script type="text/javascript">
   2:      function MakeAjaxRequest(param1, param2) {        
   3:          var url = "/Some/Site?param1=" + param1
   4:          if (param2) {
   5:              url = url + "&param2=" + param2;
   6:          }
   7:          $.ajax({
   8:              method: "POST",
   9:              url: url,
  10:              dataType: "html",
  11:              success: function(result) {                             
  12:                  showMessage("Operation succeeded");
  13:              }
  14:          });
  15:      }
  16:  </script>
Now on your link you can put:
   1:  <a href="javascript:MakeAjaxRequest('param1value', 'param2value')">Make ajax request</a>

Prevent caching javascript or css files

The reason for this post is to make an application prevent user's browser from caching css or js files when you deploy new version of this files. Here is an oryginal post for this. The goal is to make css links or javascript links in your application look like this: Listing 1: Desired css link format.
   1:  <script type="text/javascript" src="/content/js/global.js?33433651"></script>
You just have to create an helper which is going to take your application version number and add it to the parameter of css or js files source. Number with application version should change everytime you deploy your site.

środa, 2 września 2009

Automatically changing web.config files for production and development mode

The reason of this post was forced by a need to automatically change the web.config file of asp application depending on DEBUG or RELEASE version chosen when building application. The code in your web.config file can be different for example in section, where you set up web services. Listing 1 - part of web.config file - Release version
<endpoint address="https://yourdomain.com/services/serviceName.svc" binding="basicHttpBinding" bindingConfiguration="HttpsBinding" contract="ServiceProject.IService">
     <identity>
        <dns value="yourdomain.com" />
     </identity>
</endpoint>
Listing 2 - part of web.config file - development version
<endpoint address="http://localhost:65310/services/ServiceName.svc" binding="basicHttpBinding" contract="ServiceProject.IService">
   <identity>
      <dns value="localhost" />
   </identity>
</endpoint>
When you've got a lot of services in your application it can take a while to change maintain this changes during development and when releasing your application. The solution for this problem is to create 2 files. First will be called Web.DEBUG.config and will contain the same what standard web.config file contains, but with all settings needed when trying out your application on localhost. The second file is Web.RELEASE.config and differs from Web.DEBUG.config file only in this sections where things need to be set for production server. This two files should be placed in the same folder where standard Web.config file is placed. Having that done we can now set our application to change Web.config file depending on Soluction Configuration (debug or release). Right click on your web project and select Properties from context menu. Then go to tab "Build Events" and in "Post-build event command line:" window write this code: Listing 3 - Post-build event command line code
   1:  if $(ConfigurationName) == Debug goto :debug
   2:   
   3:  :release
   4:  xcopy  "$(ProjectDir)Web.RELEASE.config" "$(ProjectDir)Web.config" /Y /R
   5:  goto :exit
   6:   
   7:  :debug
   8:  xcopy  "$(ProjectDir)Web.DEBUG.config" "$(ProjectDir)Web.config" /Y /R
   9:   
  10:  :exit
I think thre's not a lot to explain. Thi code checks your Solution Configuration and replaces Web.config file with appropriate one using xcopy shell function. The paremeters are as fallows: /Y suppresses prompting to confirm you want to overwrite an existing destination file /R overwrites read-only files.