Use Both Anonymous Access and Windows Authentication#

Mad props to Craig Andera for his article on how to mix Windows Authentication with Forms/anonymous on an ASP.NET web site:

http://pluralsight.com/blogs/craig/archive/2004/07/24/1699.aspx

I'm using this at my job to allow our support staff to automatically login to our customer's applications if they access the application from within our coporate network.

Prittay, prittay, prittay cool!

10/20/2006 10:19:49 AM (Eastern Standard Time, UTC-05:00) #    Comments [0]  |  Trackback

 

ASP.NET Drop Shadow Control#

I created this control to create dropshadows on dynamic text.  I also considered using the Drawing namespace to generate an image and stream it.  I breifly even used Microsoft's IE-only filter:dropshadow css attribute.  The staw that broke the camels back on that was the fact that this filter looks terrible on XP machines with ClearType enabled.  I found an article about using a span within a div and repeating your contents twice (essentially creating a copy of your text, oversetting it, and changing its color).  I used this concept to create the server control described below.

The DropShadow control acts like a panel in the sense that it contains controls and renders them within a <div> tag.  In addition to that, however, this control captures the text "output" by all it's child controls and contents and repeats it (without any HTML tags) in such a way that it acts as a drop shadow.

Static Text Code:
<ctl:DropShadow runat="server">Search</ctl:DropShadow>

Static Text Rendering:


Static Text Rendering (Magnified):



Dynamic Control Code:
<
ctl:DropShadow runat="server"><asp:LinkButton id="lbnDisposition" Text="Status" runat="server" /></ctl:DropShadow>

Dynamic Control Rendering:


Dynamic Control Rendering (Magnified):



Notice in the second (dynamic) example that the <a> tag created by the LinkButton control is not repeated in the shadow, but the text is.

Code:

Public Class DropShadow : Inherits WebControls.Panel
  Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    writer.Write("<div style=""height:1px; display:inline; white-space:nowrap; " & _
      "font-weight:bold; position:relative; left:1px; top:1px; color:" & _
      GetHexCode(BackColor) & ";"">")

    'capture child control's output
    Dim result As New StringBuilder
    RenderChildren(New HtmlTextWriter(New IO.StringWriter(result)))
    Dim html As String = result.ToString()
    Dim rx As New Text.RegularExpressions.Regex( _
      "</?(\w+)(\s*\w*\s*=\s*(""[^""]*""|'[^']'|[^>]*))*|/?>", & _
      RegularExpressions.RegexOptions.Multiline)
    html = rx.Replace(html, "")
    writer.Write(html)

    writer.Write("<span style=""position:absolute; left:-1px; top:-1px; color:" & _ 
      GetHexCode(ForeColor) & "; width:100%;"">")
    RenderChildren(writer)
    writer.Write("</span>")

    writer.Write("</div>")
  End Sub

  Private Function GetHexCode(ByVal Color As System.Drawing.Color) As String
    Return "#" & Color.R.ToString("x2") & _
      Color.G.ToString("x2") & _
      Color.B.ToString("x2")
  End Function

  Public Sub New()
    'default colors
    Me.ForeColor = Drawing.Color.Black
    Me.BackColor = Drawing.Color.White
  End Sub
End Class
2/21/2006 1:57:51 PM (Eastern Standard Time, UTC-05:00) #    Comments [0]  |  Trackback

 

ASP.NET Session Ping#
I kept running into the situation where a user's session would timeout and cause my web applications to work differently then they expected (for instance redirecting them to the login page instead of saving the form with changes they made and left up over night).  While it's temping to say, "well, yeah, that's how web applications work," but I've always thought there should be some simple solution that doesn't require explaining session timeouts to the user.

I think I finally found one.  I call it session pinging.

At first I thought about using AJAX (Asynchronous Javascript And Xml) to do this, but I wanted to make sure this worked for a wider selection of browsers.  Plus, I had to need to feedback from the server, so AJAX seemed like it might be too much overhead for the simple need I was trying to meet.  My solution began with a brilliant little AJAX alternative I found at dotvoid.com.

function callServer(remoteScript) {
	try {
		var head = document.getElementsByTagName('head').item(0);
		var old  = document.getElementById('lastLoadedCmds');
		if (old) head.removeChild(old);
		script = document.createElement('script');
		script.src = remoteScript;
		script.type = 'text/javascript';
		script.defer = true;
		script.id = 'lastLoadedCmds';
		void(head.appendChild(script));
		return true;
	} catch (e) {
		//suppress
	}
	return true;
}

This ingenious function looks for a <script> tag in the header with and ID of lastLoadedCmds and removes it if it already exists. Then it adds a new <script> tag with the same ID and points it at a provided URL. This causes the browser to go to the URL behind-the-scenes to download it. If you specify a server-side script (ASP, ASPX, etc) instead of a static text file (.js, etc) you can run server-side, database-driven code without reloading the page. You can even have the callback page generate javascript code on-the-fly that will have some effect on the calling page (load a dropdown with values, put html into a place holder div, etc.). In other words, basically do similar things AJAX does.

Now all we have to do in an ASP.NET application to keep session alive is have every page intermitently hit any .ASPX page on the server.  EvintermittentlyerytEvery timeime this happens the session-end time is pushed back another 20 minutes (or whatever timeout you've set).  This type of intermittent action can easily be done in JavaScript with the setInterval() method.

var pingTimer;

function setPingServer(timeout) {
	pingTimer = setInterval('callServer(\'/PingServer.aspx\');', timeout);
}

function killClock() {
	clearInterval(pingTimer);
}

Now just finish up by calling the setPingServer() method in the page's onload event. Make sure you clean up the interval you set when the user leaves the page.
<body onload="setPingServer(30000)" onunload="killClock()">

This would also be a great solution to controlling the number of logged-in users for the purposes of licensing.  In that case, just change your PingServer.aspx page to log each distinct user somewhere so you can keep a count of distinct active users.
11/3/2005 11:42:39 AM (Eastern Standard Time, UTC-05:00) #    Comments [1]  |  Trackback

 

When you've had enough "pushing data"#
For all you business application developers out there, I've recently discovered the concept of Object Persistence Frameworks (OPF's) and Object-Relational Mapping (O/RM).  Basically, OPF's arose out of the fact that every business application is doing the same basic tasks: querying data from a database, persisting it temporarily in memory, displaying it to the user for viewing or editing, and persisting any user changes back to the database.  What if all these tasks could be encapsulated into a framework/object-model that would do these task for the programmer?  This persistence framework could do all the wiring-up and SQL building, and even handle null value replacement and record concurrency control.

I've recently been using Gentle.NET, an open source OPF, on two different projects.  NHibernate is another open source one that's a .NET port of the JAVA OPF called Hibernate.  I spent a couple of weeks at work setting things up so using Gentle.NET would be easy and scalable.  In only took about one week of programming to make up that time.  In other words, I'd estimate that I was able to do about 3 weeks of programming in that 1 week!  Not bad. 

Use it.  Just don't tell your boss how much extra time you'll have now.  ;)

Find out more about Gentle.NET.
10/28/2005 7:07:49 PM (Eastern Standard Time, UTC-05:00) #    Comments [2]  |  Trackback

 

Design Eye for the Usability Guy#

Web usability expert Jakob Nielsen's own site gets worked over by a few designers in this article.

6/7/2005 12:28:18 PM (Eastern Standard Time, UTC-05:00) #    Comments [23]  |  Trackback

 

Uploading large files in ASP.NET#

Chris Hynes has what I would consider "the complete solution" to the many issues related to uploading large files to an ASP.NET application.  Find out more in his krystalware blog or wiki.

6/7/2005 12:22:19 PM (Eastern Standard Time, UTC-05:00) #    Comments [27]  |  Trackback

 

All content © 2008, Jonathan Tower