WebSecurity vs FormsAuthentication in ASP.NET MVC4

16,389

Solution 1

Could possibly be related to the same issue as me MVC4 ExtendedMembershipProvider and entityframework .. I removed the universal providers nuget package and this particular error dissappeared.

Also this "very recent" article by Jon Galloway may help.

Solution 2

If you are using Visual Studio, you might want to save yourself all this effort. The MVC 4 Internet template comes with four external identity providers out of the box. I have tested them and Google Accounts, Microsoft account, Facebook login, and Twitter login all work fine, with zero lines of code!

I think the same is provided with the Web Form template too.

More info at http://blogs.msdn.com/b/webdev/archive/2012/08/15/oauth-openid-support-for-webforms-mvc-and-webpages.aspx.

Share:
16,389
Srikar Doddi
Author by

Srikar Doddi

SOLVING PROBLEMS AND MAKING THINGS WORK Technology Leadership, Technical Evangelism, Highly Scalable Software Architecture Design and Development, Service Oriented Implementation, Software Factories, Microsoft .Net Development, LAMP Development, SCRUM, Leadership & Team building, Project & Team Management, Process Optimization, Research & Intelligence, and Technology Consulting.

Updated on June 04, 2022

Comments

  • Srikar Doddi
    Srikar Doddi almost 2 years

    I guess I am trying to mix two providers in project but I am looking to use websecurity in conjunction to my forms authentication. I need websecurity for OAUTH authentication using Facebook, and google.

    The error that I am getting when I try to login using facebook is

    To call this method, the Membership.Provider property must be an instance of ExtendedMembershipProvider.

    Here are the code samples. How can I use both?

    public ActionResult ExternalLoginCallback(string returnUrl)
            {
                AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
                if (!result.IsSuccessful)
                {
                    return RedirectToAction("ExternalLoginFailure");
                }
    
                if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
                {
                    return RedirectToLocal(returnUrl);
                }
    
                if (User.Identity.IsAuthenticated)
                {
                    // If the current user is logged in add the new account
                    OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    // User is new, ask for their desired membership name
                    string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
                    ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
                    ViewBag.ReturnUrl = returnUrl;
                    return View("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = result.UserName, ExternalLoginData = loginData });
                }
            }
    

    and

    public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
    
            // If we got this far, something failed, redisplay form
            return View(model);
        }