开发者

FBA dual authentication problem

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, signinC开发者_开发技巧ontrol.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);


you should use the methode below for forms user

SPClaimsUtility.AuthenticateFormsUser(
                Context.Request.UrlReferrer,
                UserName.Text, 
                Password.Text);

and the windows part is declared like this:

protected void lbInternalUsers_OnClick(object sender, EventArgs e)
    {
        try
        {
            if (null != SPContext.Current && null != SPContext.Current.Site)
            {
                SPIisSettings iisSettings = SPContext.Current.Site.WebApplication.IisSettings[SPUrlZone.Default];
                if (null != iisSettings && iisSettings.UseWindowsClaimsAuthenticationProvider)
                {
                    SPAuthenticationProvider provider = iisSettings.WindowsClaimsAuthenticationProvider;
                    Redirect(provider);
                }
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }

    private void Redirect(SPAuthenticationProvider provider)
    {
        string comp = HttpContext.Current.Request.Url.GetComponents(UriComponents.Query, UriFormat.SafeUnescaped);
        string url = provider.AuthenticationRedirectionUrl.ToString();
        if (provider is SPWindowsAuthenticationProvider)
        {
            comp = EnsureUrl(comp, true);
        }

        SPUtility.Redirect(url, SPRedirectFlags.Default, this.Context, comp);
    }

    private string EnsureUrl(string url, bool urlIsQueryStringOnly)
    {
        if (!url.Contains("ReturnUrl="))
        {
            if (urlIsQueryStringOnly)
            {
                url = url + (string.IsNullOrEmpty(url) ? "" : "&");
            }
            else
            {
                url = url + ((url.IndexOf('?') == -1) ? "?" : "&");
            }
            url = url + "ReturnUrl=";
        }
        return url;
    }

as detailed here in the reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜