ASP.NET Forms Authentication "Remember Me" Jul 27, 2009

Another post for ASP.NET developers. By the way if you think these posts do not belong to this blog, please leave a comment, and I'll consider moving my development articles to a separate blog.
Today I needed to set up a "remember me" functionality for our web-based HelpDesk app and small-business CRM app login pages. If you ever tried to achieve this using .NET's FormsAuthentication, you might have noticed that... it's just not working. Even if you pass the "createPersistentCookie" parameter value as "true" when initializing FormsAuthentication - the cookie still lives for a limited time only - the time specified as the Forms-Authentication timeout in "web.config". Then the cookie dies.

The only solution was to increase that timeout value in web.config. And it's not a very good idea because of the security reasons.

The solution is to set the authentication cookie timeout explicitly. See the code, which is pretty self-explaining:

private void FormsAuthLogin(string userName, bool rememberMe)
{
  if (!rememberMe)
  {
    FormsAuthentication.RedirectFromLoginPage(userName, false);
  }
  else
  {
    FormsAuthentication.Initialize();
    DateTime expires = DateTime.Now.AddDays(20);
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
      userName,
      DateTime.Now,
      expires, // value of time out property
      true, // Value of IsPersistent property
      String.Empty,
      FormsAuthentication.FormsCookiePath);

    string encryptedTicket = FormsAuthentication.Encrypt(ticket);

    HttpCookie authCookie = new HttpCookie(
          FormsAuthentication.FormsCookieName,
          encryptedTicket);
    authCookie.Expires = expires;

    Response.Cookies.Add(authCookie);

    string returnUrl = FormsAuthentication.GetRedirectUrl(userName, true);
    if (string.IsNullOrEmpty(returnUrl)) returnUrl = "Default.aspx";
    Response.Redirect(returnUrl);
  }
}

Do you use Google Music Search? Jul 21, 2009

Do you use Google Music search? I know this feature has been there for a while, but I have just discovered it. When you search for some singer, Google will display a box above the results, that leads to the music-search. Like this:



But did you know that you can turn the music-search by adding "music:" in front of your query? Like this:

"music:u2"

This will force Google to look in his music indexes.

Using Google Reader to track your product on Twitter Jul 13, 2009

Dennis Crane gave a great advice on how to track your product mentioned on Twitter:

Twitter has a great feature called "search feed" - a search-engine results page published as a feed. So simply add this feed to Google Reader (or any other feedreader):

http://search.twitter.com/search.atom?q=product+name

and stay updated.

Calling ASP.NET Web Services from jQuery Jul 7, 2009

Another post for ASP.NET developers reading this blog (if any).

I prefer compiling our web-projects to ASP.NET 2.0 rather than 3.5 basically because of all that stuff Visual Studio adds to the "Web.config" file. And the "Web.config" is often edited by our end-users, who might find it confusing to make their way through all these "configSections", "assemblies" and "httpHandlers" that look quite scary.

With the latest project we're working on (a CRM and contact management application) we decided to finally benefit from the MS AJAX framework that is built in to the .NET Framework 3.5.

But to optimize our code and keep it lightweight and fast, we've decided to use jQuery where possible, avoiding the bulky and clumsy MS AJAX's javascript.

So - how do you use jQuery to call a JSON ASP.NET web-service? Here it is. The code is self-explaining:

WebService code:

[WebService(Namespace = "http://mynamespace.org/")]
//the next line is important
[System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(int a, string b)
{
return "Hello World";
}
}

Now the client-side code. Note the JSON-serialized parameters passed with the "data" property:

$.ajax({
type: "POST",
url: "MyWebService.asmx/HelloWorld",
data: '{a:1,b:"test"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
}
});


Blog Archive