WebSecurity VS在ASP.NET MVC4 FormsAuthentication [英] WebSecurity vs FormsAuthentication in ASP.NET MVC4

查看:138
本文介绍了WebSecurity VS在ASP.NET MVC4 FormsAuthentication的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜我试图混在项目中的两个供应商,但我期待使用websecurity一起到我的窗体身份验证。我需要使用Facebook的进行OAuth认证websecurity,和谷歌。

这是我得到的错误,当我尝试使用Facebook是登录

要调用这个方法,在 Membership.Provider 属性必须是一个实例 ExtendedMembershipProvider

下面是code样品。我怎么能同时使用?

 公众的ActionResult ExternalLoginCallback(字符串RETURNURL)
        {
            AuthenticationResult结果= OAuthWebSecurity.VerifyAuthentication(Url.Action(ExternalLoginCallback,新的{RETURNURL = RETURNURL}));
            如果(!result.IsSuccessful)
            {
                返回RedirectToAction(ExternalLoginFailure);
            }            如果(OAuthWebSecurity.Login(result.Provider,result.ProviderUserId,createPersistentCookie:FALSE))
            {
                返回RedirectToLocal(RETURNURL);
            }            如果(User.Identity.IsAuthenticated)
            {
                //如果当前用户登录添加新帐号
                OAuthWebSecurity.CreateOrUpdateAccount(result.Provider,result.ProviderUserId,User.Identity.Name);
                返回RedirectToLocal(RETURNURL);
            }
            其他
            {
                //用户是新的,问他们需要的会员名称
                字符串loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider,result.ProviderUserId);
                ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
                ViewBag.ReturnUrl = RETURNURL;
                返回视图(ExternalLoginConfirmation,新RegisterExternalLoginModel {用户名= result.UserName,ExternalLoginData = loginData});
            }
        }

 公众的ActionResult登录(LoginModel型号,串RETURNURL)
    {
        如果(ModelState.IsValid)
        {
            如果(Membership.ValidateUser(model.UserName,model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe);
                如果(Url.IsLocalUrl(RETURNURL))
                {
                    返回重定向(RETURNURL);
                }
                其他
                {
                    返回RedirectToAction(指数,家);
                }
            }
            其他
            {
                ModelState.AddModelError(,提供的用户名或密码不正确。);
            }
        }        //如果我们走到这一步,事情失败了,重新显示形式
        返回查看(模型);
    }


解决方案

也可能会被涉及到相同的问题我 MVC4 ExtendedMembershipProvider和的EntityFramework
..我删除了通用的供应商的NuGet软件包,此特定错误dissappeared。

另外这款<一个href=\"http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx\"相对=nofollow>非常近由Jon加洛韦的文章可以帮助。

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);
    }

解决方案

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.

这篇关于WebSecurity VS在ASP.NET MVC4 FormsAuthentication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆