Blog Archives

Change Login Using Windows Authentication

Recently I had a need to allow users to change their login on an MVC site that used Windows authentication.  Of course we did not want the user to log out of their machine to do it.  The trick of course involved sending a 401 response, but how to do that and not get stuck in an endless loop.

The 401 Loop

It seemed simple enough, in fact too simple.  You just return a 401 challenge and have them move on to their previous page.  The 401 response is what presents the user with the Windows login popup.  In reality the 401 response acts like a redirect on itself, so you get two page loads and it forgets any variables you set.

Well that simply would not do. Read the rest of this entry

Using LINQ to Sort by Day of Week And Time

So a couple weeks ago I saw a question on the ASP.Net or MSDN forums.  Someone wanted to know how to use LINQ to sort by the day of the week and then by the time for a given datetime.  I did not have a chance to do anything with it, but for some reason it stuck in my head.  So, I came up with the answer, but of course I do not remember where the post was located.  The answer is very simple.

Read the rest of this entry

Viewing Your Proxy Class’ SOAP

Most of us are a bit spoiled with .NET and its lovely ability to take a WSDL (via the WSDL Tool) and create us a proxy class.  We can then use this object to work with the web service.  What if you need to actually see that XML?

Well, Microsoft has a solution.  You can use an extension method to log your SOAP.  Not sure how to do it, don’t worry about that either, Microsoft has already written it for you.

Please go to this link to read the full article.

Custom Validation Controls

Custom validation controls are a nice tool that you can use when the standard validation controls just won’t do the job.  Most examples I found out there only addressed the server side validation aspect.  Don’t get me wrong, you can do that, just use server side, but ideally you want to do both.

Trust me though, if you only want to do one side, do server side.  You will want to always do server side as a minimum just in case the user has javascript disabled or something.  Server side only is kind of old school but it gets the job done very reliably.

Read the rest of this entry

The Conditional Operator (?:)

I don’t use this thing too much, so I often forget it when I need it.  In a nut shell it is a nice one line if statement.  You can view the MS page here.

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.
The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated.

condition ? first_expression : second_expression;

Let me provide a realistic example.  Lets say you have a method to generate a random number from 1 – 4, and you want to see if the number is odd or even.


private int RandomNumber(int min, int max)
{
    Random random = new random();
    return random.Next(min, max);
}

private string NumberIs()
{
    string sNumberIs = (RandomNumber(1, 4) % 2 == 0) ? "Even" : "Odd";
    return sNumberIs;
}

Web User Control Events

Ever want to have an event for a Web User Control that the parent page can consume?  Well you can, and it is fairly simple.

I am not an expert since I just figured this out myself so here we go.

There are three basic things you need to do for each event.

  1. Create a public delegate variable in your User Control
  2. Create a public event event for the type of delegate
  3. Create a protected virtual method

So lets see some code.

//Public variables and methods
public delegate void ItemClickEvent(object sender, EventArgs e);
public event ItemClickEvent ItemClick;

//Protected Methods
protected virtual void OnItemClicked(object sender, EventArgs e)
{
    if (ItemClick != null)
    {
        ItemClick(this, e);
    }
}

The property in the page with this user control will be called OnItemClick.  Notice I do not have anything named OnItemClick.  The only public names are ItemClickEvent, which is a delegate and the event itself, ItemClick.  So I assume that .NET adds the On to the front of your event name.