HeartattacK

September 2008 - Posts

Setting Timeouts for Session and Authentication

Setting the timeouts for Session and Authentication can be a bit tricky. If not set properly, your user may be logged in when the Session expires. If you’re app depends on Session specific to the logged in user, you’ll have problems. By default, the Authentication timeout is 30 minutes while for that of Session is 20 minutes. Here’s how you can change that on a site wise basis:

Session

Under <system.web>, add this node:

<sessionState timeout="30"></sessionState>

Authentication

Under <system.web>, have this node:

<authentication mode="Forms">
      <forms timeout="20"></forms>
</authentication>

For both, the timeout is in minutes. Also, note that there are a host of other settings you can change for Authentication, which you can set within the <forms> tag. Intellisense does work.

A lot of people think setting the Session timeout will ensure that the user gets logged out after the Session expires. This is probably because of the fact that there’s no timeout in the <authentication> node. The reason that there isn’t one is because there are two types of authentication accessible in the <authentication> section: 1) Forms and 2) Passport. There can be different settings for each, and hence the timeout needs to be set in the <forms> tag under the <authentication> tags.

Hope that helps.

Posted: Sep 29 2008, 11:59 AM by HeartattacK | with 2 comment(s) |
Filed under:
JQuery is now 1st Party for Asp.net - Kinda

Wow...AMAZING…Hurray…I mean, like, WOW man…

In a very welcome turn of events, JQuery is now “officially” going to be part of Asp.net development. MS will provide support, annotated VS versions that’ll give us intellisense, provide us with demonstrations and tutorials on how jquery can work with features like the AJAX Control Toolkit, Asp.Net AJAX, client side templates – and all the time without modifying, forking or otherwise changing jquery. This is surprising, and in a very pleasant way.

When I looked at the Asp.net AJAX Roadmap a while back, I thought that a lot of it was what jquery does (css selectors, anyone?). I actually liked the idea as I use JQuery a lot, and it appeared a lot of the neat features were headed our way. I was grateful cause I do believe community interest in jquery is what put those features in the roadmap. I thought that was the best we could expect…and I thought the only “better” move would be for MS to buy jquery.

Well, MS seems to have done even better with this move. Instead of killing it off, they’ve joined it and are going to make it an even better experience using jquery alongside asp.net. This works so well for the “Dark Empire” persona of MS and integrates so nicely into the ecosystem. Well done, bravo, outstanding…can’t say it enough time really.

Scott Hanselman has put up a tutorial already on how this’ll work with Astoria (ok…ADO.Net data services, but Astoria is such a cool name!) and client templates. I’ve been using asp.net AJAX and jquery together for some time now. And I can say it works exactly like I expected it to work. No need to re-learn MS-specific jquery, no hassle – it just works.

And lastly, MS will provide bug reports, patches, tests etc. to the jquery project and “These will all go through the standard jQuery patch review process.

  • “If you can’t beat ‘em…buy ‘em”.
  • “If you can’t beat ‘em…join ‘em.”
  • “If you can’t beat ‘em…support ‘em and make ‘em even better for your own benefit.”

I never imagined option 3 was even possible for MS. All I can say is I’m jumping for joy!!

LiveWriter Test

Checking out the new wave 3 beta of Windows Live. Love the new messenger(v9) so far…now testing LiveWriter. Looking good.

Disable Caching in an HttpHandler

I was generating some custom reports in Word 2007 format today. The reports were being served by an HttpHandler and various params are passed to it (mostly by query string). One report needed a list of ids to be passed and the query string wasn't an option there, so I put that in Session. [My other post today shows how]. The trouble was that the urls were identical and someone clever (the browser or the server) was caching the report. So, changing the parameter that was made up of ids resulted in no change of the report. Now, output caching is pretty simple to eliminate on pages, and for asmx web services for that matter, but I found that doing so for a handler is slightly tricky. Here's what I did:

context.Response.Clear();context.Response.Cache.SetCacheability(HttpCacheability.Public);

context.Response.Cache.SetExpires(DateTime.MinValue);

 in the ProcessRequest method.

I would've thought context.Response.Cache.SetCacheability(HttpCacheability.None) would do it, but that kept giving me errors when downloading the docx file. Rather, enabling cacheing and forcing a timeout seems to do it.

 

Hope that helps.

Posted: Sep 18 2008, 05:56 PM by HeartattacK | with 4 comment(s) |
Filed under: ,
How to use Session values in an HttpHandler

When writing a custom HttpHandler, by default you have no access to the Session object. Doing something like HttpContext.Current.Session also returns null. The workaround is quite simple:

Reference the System.Web.SessionState namespace:

using System.Web.SessionState;

 ...and decorate the handler with either the IRequiresSessionState attribute:

public class MyHandler:IHttpHandler, IRequiresSessionState 

or the IReadOnlySessionState attribute:

public class MyHandler:IHttpHandler, IReadOnlySessionState

with the latter giving read only access to the seesion object.

 

Hope that helps.

 

EDIT: As pointed out...IReadOnlySessionState and IRequiresSessionState are not attributes, but empty interfaces. This is pretty apparent from the fact that we're not decorating the handler, rather the handler implements the interface [and since it's an empty interface, we don't need to implement anything to do so]. Late night blogging can lure the fingers to strange routes on the keyboard, it seems 8)

Posted: Sep 18 2008, 03:30 PM by HeartattacK | with 13 comment(s)
Filed under: , ,
FTP Woes and....FireFTP to the Rescue!!

I'm running Vista x64 sp1. I've had IE 7 and then IE 8 (b2) installed. I tried uploading via ftp from a)Windows Explorer, b)Visual Studio ftp, c)FireZilla, d)SmartFTP. Small files uploaded perfectly but even 150KB files kept timing out and retrying. Active/passive didn't help. Nothing helped. I tried for 28 hours to upload a 340KB file. It was infuriating. I tried from another ISP's connection. I tried from XP. Nothing worked. God knows why. I contacted tech support, they said it was a problem on my ISP and that my connection was poor. While I agree a 100kbps upload isn't good at all, I do expect to upload 100KB in a few minutes and not failing for hours. I can't be too sure about their response as people using my ISP DO upload via ftp. I sent my files to a friend in the US and he graciously uploaded for me. He mentioned it was slow. And then I needed changes on my site. Enter frustration.

 Well...just now I found a brilliant solution that's just plain mind boggling. I learned of FireFTP, an ftp plugin for FireFox. Now, (this may surprise a lot of folks), I don't like FireFox much. On my dev machine, it's painfully slow loading pages from the VS dev server compared to IE7 / IE8 / Chrome. But this is one thing I'll always be using for ftp from now on. You see, FireFTP is slick, fast and it uploaded my 350KB file in 25 seconds flat(!) - something the bigger players failed to do.

 I'm guessing this is coz of some OS configuration or firewall or whatever. I tried turing off vista auto tuning. Nothing helped. I faced the same problem from XP though. I wonder if it's my good buddy Kaspersky that's causing me the pain. I did try turning off the firewall, but that didn't help either.

Anyhow, I'm glad I found fireftp. I noticed some people on the forums were complaining about slow ftp too. I hope fireftp can help them too.

 

 

EDIT: As Josh points out...I really should have posted this link here:

http://fireftp.mozdev.org/

FireFTP: The BEST and SIMPLEST FTP software I've EVER used - and one that just "works".

There you go :D

HTML Comments, Other Comments and Some VS Tips

 

I just found out something really weird about HTML comments. Apparently, according to standards, you can't do this:

<!--  comment ------------------------ -->

That is, you can't put two or more ‘-‘s inside a comment. It works as expected in IE7, but it breaks down horribly in FireFox. FireFox simply renders the comment to the user, which is pretty useless as that's not what comment are for!!. The standards "recommend" developers to not put two or more consecutive ‘-‘s inside an html comment. Now, that's weird. If there are specific delimeters like <!-- and  -->, then I should be able to use any part of those delims (but not the whole) inside a comment, shouldn't I?

Since, we're in the vicinity of comments, let's look at some for reference:

HTML: <!-comment -->

C#: //single line comment

/* multiline comment */

VB.NET: ‘single line comment

SQL: /* multiline comment */

-- single line comment

Asp.Net markup (aspx): <%-- comment --%>

 

Now here's a neat trick regarding commenting and uncommenting code in Visual Studio and Visual Web Developer. In any code editor, highlighting a bit of code (be it VB.Net, C# or aspx markup), if you press ctrl+(k, c) [That is, press k and then c while holding down control), it'll automagically comment out that part using the comments that apply to the selected code (so, // in C# and ‘ in VB.Net etc). To uncomment, highlight and hit ctrl+(k,u). One thing to keep note is that the uncomment will work if you select any part of the beginning of a line for VB.Net and C#. For aspx markup though, you must select from <% to the closing %> for the ctrl+(k,u) uncommenting to work.

Also, asp.net <%-- --%> comments work server side. So, nothing in them are visible to the compiler. HTML comments work client side. So, if you put anything inside <!-- -->, it'll be visible to the compiler. Hence, if you're trying to comment out some part of markup that fails to compile, you must use the server side <%-- --%> comments.

 

One last trick for today is the auto formatting of code. In a code editor in Visual Studio or Visual Web Developer, you can select some code and hit ctrl+(k,f). This'll auto format that part of the code. This is immensely helpful for formatting aspx markup really quickly. Keep in ind though, that your code must be valid and error-free. If the ctrl+(k,f) trick doesn't work, it's a sign that there's a problem with code/markup.

Hope that helps.

More Posts