Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

What I have?

I have configured FBA in one of the web applications with out of the box login page having dropdown box to select the either windows or FBA login. Everything is working fine.

What I want?

I want to have a custom login page having text boxes for Username and Password and a login button which will be used for authenticating both Windows and FBA users. To distinguish between the two different logins, I want to handle OnAuthenticate event and check if the user name contains a '\' then assume it is Windows user otherwise, it is FBA user.

This is the code written in OnAuthenticate event handler:

protected void signinControl_Authenticate(object sender, AuthenticateEventArgs e)
{
    string fullUserName = signinControl.UserName;
    string username = null;

    if (fullUserName.Contains("\\")) //Windows user
    {
        string domain = fullUserName.Substring(0, fullUserName.IndexOf("\\"));
        using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain))
        {
            username = fullUserName.Substring(fullUserName.IndexOf("\\") + 1);
            e.Authenticated = pc.ValidateCredentials(username, signinControl.Password);
        }
    }
    else //FBA user
    {
        e.Authenticated = Membership.ValidateUser(fullUserName, signinControl.Password);
    }
}

What problem am I facing?

The code above works well for FBA Users. But, when I try to login with a windows user, even though the e.Authenticated is set true after validating, it is throwing this error: "Your login attempt was not successful. Please try again.".

e.Authenticated = pc.ValidateCredentials(username, signinControl.Password);

I believe that, setting e.Authenticated to true should redirect the user from login page to the requested page. Can someone please help me if I have to do anything else to get Windows user signed in?

Update-1:

I used SetAuthCookie() method to set Cookie explicitly, still the same result.

FormsAuthentication.SetAuthCookie(username, true);
share|improve this question
    
Did you find a solution to your problem, i'm facing the same issue. –  moi_meme Oct 14 '11 at 20:09

3 Answers 3

They really shouldn't have to re-enter their credentials if they want to sign-on using Windows Auth, so I would use the OOB implementation (i.e. redirect to the OOB Windows login page) to perform the Windows authentication...put this in a code-behind for a button or link:

string components = System.Web.HttpContext.Current.Request.Url.GetComponents(UriComponents.Query, UriFormat.SafeUnescaped);
Microsoft.SharePoint.Utilities.SPUtility.Redirect("/_windows/default.aspx", Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, this.Context, components);

So change your custom page to put a link or button on the page to "Login with your Windows Credentials"...clicking on that calls the code above, logs them in, and redirects them back to the page that brought them to login page in the 1st place.

share|improve this answer
    
I think it is almost same as OOB Login page. The thing here is that it is little hard for the users of this site to make a differentiation between Windows and FBA users. So, I thought of doing this in the code by having a single means of login to both Windows and FBA users. –  Vijay Aug 8 '11 at 6:03
1  
There's nothing stopping you from redirecting them to the Windows auth page if they put domain\username...you're essentially ignoring the entry, but it would log them into SharePoint if they are already logged into the domain via Windows. You could also detect if they are already logged into Windows, display a "login to SharePoint as domain\username" link, and redirect to the Windows auth page in the code-behind. –  Rob D'Oria Aug 8 '11 at 15:31
    
It's showing the credential popup when I have "Prompt for user name and password" set in IE. Is it possible to perform these actions through code which the credential popup does? –  Vijay Aug 9 '11 at 4:26

One possible way to create a single interface to login both types of users is to look up the username in both the windows and FBA providers. Here is how the Windows check would work. This can be repeated changing to the analogous classes for Forms Based Authentication.

private SPWindowsAuthenticationProvider myWindowsProvider;
SPIisSettings iisSettings = SPContext.Current.Site.WebApplication.IisSettings[SPUrlZone.Default];

        if (null != iisSettings && iisSettings.UseWindowsClaimsAuthenticationProvider)
        {
            myWindowsProvider = iisSettings.WindowsClaimsAuthenticationProvider;
        }

The SPWindowsAuthenticationProvider is then used to get a MembershipProvider so you can get to the MembershipUser object and verify they exist.

MembershipProvider myProvider = System.Web.Security.Membership.Providers[myWindowsProvider.MembershipProvider];
MembershipUser myWindowsUser = myProvider.GetUser(this.signInControl.UserName, false);

Just check if the myWindowsUser is null. If it is not null, the preferred method is to send the user to the /_windows/default.aspx using the code Rob provided above. If it is null, then do your forms authentication. This may also fail if someone entered a bad username, so you need handle this case as well.

share|improve this answer
    
Redirecting to /_windows/default.aspx shows the popup for entering windows credentials when I have "Prompt for user name and password" set in IE. Is it possible to perform these actions through code which the credential popup does? –  Vijay Aug 9 '11 at 4:30
    
The only time I see a popup for windows account is when I use Chrome. The other time I saw it was when basic authentication was enabled on the IIS site. Could this be the case for you. If configured correctly, authentication for a windows user is seamless. –  Rich Ross Aug 9 '11 at 11:49

Agree with Rob's suggestion. I implemented it slightly differently using an 'a' tag in the html for my custom login page. The authentication will happen in the /_windows/default.aspx page so no need to handle it in your code.

share|improve this answer
    
I think it is almost same as the OOB Login page. The thing here is that it is little hard for the users of this site to make a differentiation between Windows and FBA users. So, I thought of doing this in the code by having a single means of login to both Windows and FBA users. –  Vijay Aug 8 '11 at 6:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.