About UsCommunityTrainingContent DevelopmentContact

Blogs
Pluralsight
Course Schedule
Scott Allen
Craig Andera
Mark Baciak
Don Box
Keith Brown
John CJ
Tim Ewald
Jon Fancey
Jon Flanders
Vijay Gajjala
Kirill Gavrylyuk
Ian Griffiths
Martin Gudgin
Jim Johnson
John Justice
Mike Henderson
Joe Hummel
Matt Milner
Ted Neward
Fritz Onion
Brian Randell
Jeffrey Schlimmer
Aaron Skonnard
Dan Sullivan
Herb Sutter
Doug Walter
Jim Wilson
Mike Woodring

My Links
Home
Contact
Login

Blog Stats
Posts - 601
Stories - 0
Comments - 2438
Trackbacks - 260

Navigation
Craig's Wiki(rss)
Direct3D Tutorial(rss)
Technorati Profile

Archives
Apr, 2008 (2)
Mar, 2008 (4)
Feb, 2008 (3)
Jan, 2008 (4)
Dec, 2007 (2)
Nov, 2007 (2)
Oct, 2007 (2)
Sep, 2007 (8)
Aug, 2007 (2)
Jul, 2007 (3)
Jun, 2007 (2)
May, 2007 (3)
Apr, 2007 (5)
Mar, 2007 (5)
Feb, 2007 (1)
Jan, 2007 (4)
Dec, 2006 (1)
Nov, 2006 (7)
Oct, 2006 (3)
Sep, 2006 (3)
Aug, 2006 (1)
Jul, 2006 (3)
Jun, 2006 (5)
May, 2006 (3)
Apr, 2006 (7)
Mar, 2006 (1)
Feb, 2006 (6)
Jan, 2006 (1)
Dec, 2005 (5)
Nov, 2005 (6)
Oct, 2005 (8)
Sep, 2005 (4)
Aug, 2005 (7)
Jul, 2005 (3)
Jun, 2005 (8)
May, 2005 (6)
Apr, 2005 (9)
Mar, 2005 (12)
Feb, 2005 (8)
Jan, 2005 (9)
Dec, 2004 (9)
Nov, 2004 (6)
Oct, 2004 (10)
Sep, 2004 (7)
Aug, 2004 (14)
Jul, 2004 (14)
Jun, 2004 (11)
May, 2004 (16)
Apr, 2004 (16)
Mar, 2004 (17)
Feb, 2004 (15)
Jan, 2004 (18)
Dec, 2003 (16)
Nov, 2003 (23)
Oct, 2003 (15)
Sep, 2003 (14)
Aug, 2003 (19)
Jul, 2003 (20)
Jun, 2003 (24)
May, 2003 (32)
Apr, 2003 (35)
Mar, 2003 (30)
Feb, 2003 (30)
Jan, 2003 (12)

Post Categories
(rss)
Articles(rss)
Downloads(rss)


Craig Andera's Weblog

A client of mine wants to provide single sign-on (SSO) capabilities in their web application, so that users don't have to type in their domain password when authenticating to the application. The twist? Only some users of the application use SSO: the rest have accounts that exist only in the application database. So we couldn't just flip on “integrated authentication” in IIS and party on. But with the help of Keith Brown, I was able to figure out a pretty nifty solution.

The trick was realizing that if you enable both “anonymous“ and “integrated“ authentication for a particular virtual directory, the browser won't try to authenticate to the web server until it receives a 401 (Unauthorized) back from the web server. But you can issue your own 401 any time you like! So what I did was to just set up Forms authentication as normal, but also provided a checkbox on the login form that said, “Use my network credentials.” Then, in my login form, I did something like this:

public class Login : Page {
  protected
Label ErrorMessageLabel;
  protected
TextBox UsernameTextBox;
  protected
TextBox PasswordTextBox;
  protected
CheckBox CheckBox1;

  public void Page_Load(object
o, EventArgs e) {
    if
(IsPostBack) {
      string authenticatedUser = null
;
      if (CheckBox1.Checked)
// Use their network credentials
     
{
        string
user = Request.ServerVariables["LOGON_USER"];
        if (user.Length == 0)
// They haven't provided credentials yet
       
{
         
Response.StatusCode = 401;
          Response.StatusDescription = "Unauthorized";
          Response.End();
        }
        else
// They have
       
{
         
authenticatedUser = user;
       
}
      }
      else
// Use the username and password they provide
     
{
         if
(IsPasswordOK(UsernameTextBox.Text, PasswordTextBox.Text)) {
          authenticatedUser = UsernameTextBox.Text;
        }
      }
      if (authenticatedUser != null)
// They authenticated successfully
     
{
        // Issue the Forms Auth cookie and send them on their way

        FormsAuthentication.RedirectFromLoginPage(authenticatedUser, false
);
      }
      else
// They didn't
     
{
       
ErrorMessageLabel.Text = "Invalid username or bad password. Please try again.";
     
}
    }
  }
}

What this does is - when the user submits the login - check to see whether they want to authenticate by providing a username and password (normal Forms authentication) or whether they want to authenticate automatically, using their logged-in credentials. Right now, I'm figuring this out by having them explicity check a checkbox, but I do lots of other things. For example, I could have them always enter their username, and then go look in the database to see whether they're supposed to get a SSO login or a normal one. Or I could have them check the checkbox once and remember their settings forever after in a cookie.

Whatever mechanism I decide on, the trick here is that I can force the browser to authenticate by sending back a 401. Then, in subsequent visits, I can check the LOGON_USER server variable to see if the authentication was successful or not. If it is, I'm perfectly welcome to issue them a valid Forms Authentication login, secure in the knowledge that the user has proven knowledge of their password to IIS already.

If the user is using IE, the authentication will happen automatically, using whatever credentials they're logged in to the client machine with. It works in Firefox, too, but they get that little username/password popup dialog box. Oh well - maybe the Firefox people will add auto login in a future release, or someone will write an extension. But failing that, providing SSO only to IE users is good enough for us.

posted on Saturday, July 24, 2004 8:40 AM

  • # Mixing Forms and Windows Authentication
    Darrell Norton's Blog
    Posted @ 7/26/2004 9:25 AM
    Mixing Forms and Windows Authentication
  • # RE: Mixing Forms and Windows Authentication
    Christopher
    Posted @ 7/27/2004 1:59 PM
    That, sir, is completely hardcore. :) Slick.
  • # re: Mixing Forms and Windows Authentication
    Craig
    Posted @ 7/27/2004 2:27 PM
    I aim to please. ;)
  • # re: Mixing Forms and Windows Authentication
    Dimitri Glazkov
    Posted @ 8/18/2004 11:54 AM
    Craig,

    This is pretty cool -- you could make this even better by using my Credentials Screening solution (http://glazkov.com/blog/archive/2004/06/06/189.aspx). That way, you won't even need to see a login form if your network credentials are valid.

    :DG<
  • # re: Mixing Forms and Windows Authentication
    Craig
    Posted @ 8/18/2004 12:57 PM
    I'm not sure I understand. This is an IE-only solution, and IE already doesn't pop up a dialog box.

    In order to never display a login form, you have to catch the second 401 that happens when they provide network credentials that are no good, or fail to provide any to your first challenge. And the problem with that is, the dialog box that pops up for non-IE users pops up after the first challenge, so you'd still have that.

    I guess what you'd have to do is to figure out based on the user-agent string which browser they're using, and react accordingly. Assuming they haven't used one of the many available plugins to make it lie to you. :) If you did that, though, you'd be able to skip displaying the form and go straight to the challenge.

    All in all, it would be a moderately complicated piece of code.
  • # re: Mixing Forms and Windows Authentication
    Dimitri Glazkov
    Posted @ 8/18/2004 1:36 PM
    IE doesn't always let you slip through withouth the dialog box. For example, if you are accessing the site that is not in your Local Intranet zone, you will get the dialog box no matter what. Credentials Screening solution I proposed addresses this exact problem. Otherwise, in your solution, even after you check the "use network credentials" checkbox, you will a standard security dialog box unless the site in Local Intranet zone, which may or may not be the case, depending on the configuration of the network.

    I agree, it adds complexity, but it also adds completeness :)
  • # re: Mixing Forms and Windows Authentication
    Craig
    Posted @ 8/18/2004 1:54 PM
    Interesting! I didn't know that about the Intranet Zone!

    Thanks for explaining. :)
  • # re: Mixing Forms and Windows Authentication
    Dimitri Glazkov
    Posted @ 8/19/2004 8:01 AM
    Not a problem. Now I can go tell people for a week that I taught Craig Andera something :)
  • # re: Mixing Forms and Windows Authentication
    Craig
    Posted @ 8/19/2004 8:29 AM
    Heh. I think you need some new goals: finding something I don't know is too easy. :)
  • # Better Credentials Screening
    Dimitri Glazkov
    Posted @ 10/4/2004 8:36 AM
  • # re: Mixing Forms and Windows Authentication
    Support Guy
    Posted @ 6/7/2005 11:26 AM
    Interestingly enough, I just read this blog: http://ackbarr.xoops.org/archives/2005/03/31/integrated-windows-authentication-in-firefox/
    Getting Windows Authentication working in Firefox
    If you have control of the browser settings (it's in user.js in Firefox) you can make it a default setting.
    This is a pretty old blog so this may not be relevant info anymore.
    Just trying to help out.
  • # re: Mixing Forms and Windows Authentication
    Craig
    Posted @ 6/7/2005 11:34 AM
    Interesting - I'll have to check that out. I don't think it helps my client, but it might help *me*, which is more important. :)

    Thanks!
  • # Better Credentials Screening
    Dimitri Glazkov
    Posted @ 7/5/2005 8:27 AM
  • # Windows &amp; Forms Authentication
    B³: Beto Borbolla Blog
    Posted @ 7/18/2005 4:31 PM
    Interesante tip de como proveer un proceso de autentificación integrada tanto para usuarios que puedes...
  • # Mixing Forms and Windows Authentication in ASP.NET 2.0
    CraigBlog
    Posted @ 6/19/2006 12:06 PM
  • # re: Mixing Forms and Windows Authentication
    JTower
    Posted @ 10/20/2006 9:27 AM
    Brilliant work! Thanks for posting this, Craig. It was exactly what I was looking for.
  • # re: Mixing Forms and Windows Authentication
    Doug
    Posted @ 9/17/2007 9:14 AM
    Craig or Dimitri,
    Do you still have the code for the credentialing screening available? Looks like the link above no longer exists and I would like to look closer at Dimitri's code.

    Thanks in advance.
    Doug
  • # re: Mixing Forms and Windows Authentication
    Doug
    Posted @ 9/17/2007 9:15 AM
    I'm trying to use the above (which is awesome) and avoid the login pop-up but rather gather the user context since they have already logged into the domain.
  • # re: Mixing Forms and Windows Authentication
    Craig
    Posted @ 9/17/2007 9:47 AM
    Which link are you talking about that no longer exists?
Title  
Name  
Url
Comments   
Please enter the code you see below. what's this?
This CAPTCHA image helps deter automated scripts that submit comment spam. In essence, it helps us determine that you are indeed a human instead of script.

 
   
 
© 2004 Pluralsight.
Visual Design by Studio Creativa
Privacy Policy