Thursday, June 20, 2013

Predicates- small but beautiful

 
A predicate is a function that returns true or false & a predicate delegate is a reference to a predicate.
So basically a predicate delegate is a reference to a function that returns true or false. Predicates are very useful for filtering a list of values - here is an example.
 
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
     List list = new List { 1, 2, 3 };

     Predicate predicate = new Predicate(greaterThanTwo);

     List newList = list.FindAll(predicate);
    }

    static bool greaterThanTwo(int arg)
    {
     return arg > 2;
    }
}
Now if you are using C# 3 you can use a lambda to represent the predicate in a cleaner fashion:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
     List list = new List { 1, 2, 3 };

     List newList = list.FindAll(i => i > 2);
    }
}
 
OR use the plane old inline Function delegate:
 
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List list = new List { 1, 2, 3 };

        List newList = list.FindAll(delegate(int arg)
                           {
                               return arg> 2;
                           });
    }
}

Friday, March 29, 2013

Signing Third Party Dll's

Faced a problem today where I was not able to refer a third party dll (MessageGearsSDK.dll) into my project.
Problem was simple- My project dll is signed but MessageGearsSDK.dll is unsigned (Shame on them to release a non-signed dll for redistribution).

Binged a lot and then slapped myself when I found that the solution was also so simple (shame on me for not able to think it by myself) -

Use Ildasm to create .il file and then recreate the dll by signing it with my own key :-)

ildasm /all /out=MessageGearsSDK.il MessageGearsSDK.dll
ilasm /dll /key=xxxx.snk MessageGearsSDK.il

For more information and some exceptions (like, you cannot do this for a assembly which uses a un-managed code or uses delay signing), please refer following link-

http://buffered.io/posts/net-fu-signing-an-unsigned-assembly-without-delay-signing/

Tuesday, February 19, 2013

Sliding expiration explained

Sliding expiration was always a confusion to me unless I decided to dig inside the code.
This is what happening inside

Declaring Type:
System.Web.Security.FormsAuthentication
public static FormsAuthenticationTicket RenewTicketIfOld(FormsAuthenticationTicket tOld)
{
    if (tOld == null)
    {
        return null;
    }
    DateTime utcNow = DateTime.UtcNow;
    TimeSpan span = (TimeSpan) (utcNow - tOld.IssueDateUtc);
    TimeSpan span2 = (TimeSpan) (tOld.ExpirationUtc - utcNow);
    if (span2 > span) <--------So it will renew only after mid of the total timeout minutes
    {
        return tOld;
    }
    TimeSpan span3 = (TimeSpan) (tOld.ExpirationUtc - tOld.IssueDateUtc);
    DateTime expirationUtc = utcNow + span3;
    return FormsAuthenticationTicket.FromUtc(tOld.Version, tOld.Name, utcNow, expirationUtc, tOld.IsPersistent, tOld.UserData, tOld.CookiePath);
}
Above function get called by OnAuthenticate of System.Web.Security.FormsAuthenticationModule

{
…...
          if (FormsAuthentication.SlidingExpiration)
                {
                    ticket = FormsAuthentication.RenewTicketIfOld(tOld);
                }
…….
}


This implementation makes some sense because Authentication ticket's Expiration property doesn't allow it to set, so we need to create new authentication ticket.
But for me, I have to problem using slidingExpiration-
It messed up my 'about to timeout' popup (I have a SPA) at browser (although I can fix it now after understanding this logic)
It's hard to tell the QA guy that if you ideal for 16 minutes (in a timeout of 30 minutes), you will be logged-out . And all this is because ticket is not renewed when you sent your get/post at 14th minute :-(

Looks like I can’t win that battle (with QA and business) so I need to let go slidingExpiration (by setting it to false) and manually renew (recreate) the ticket for each request :-(

Wish they had allowed expiration property of FormAuthenticationTicket so that I need not to recreate the whole ticket again and again…

Labels