ASP.NET身份+ Facebook登录:进入“重新请求” [英] ASP.NET Identity + Facebook login: Pass in "rerequest?"

查看:254
本文介绍了ASP.NET身份+ Facebook登录:进入“重新请求”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(在Web API项目中使用ASP.NET Identity 2.1,Microsoft.Owin.Security.Facebook 3.0.1)

(Using ASP.NET Identity 2.1, Microsoft.Owin.Security.Facebook 3.0.1 in a Web API project)

从这里: https://developers.facebook.com/docs/facebook-login/ login-flow-for-web / v2.2

这是因为一旦有人拒绝了权限,登录对话框请求他们,除非你明确告诉对话框你要求被拒绝的许可。

你可以通过添加auth_type :rerequest flag to your FB.login()call:

FB.login(
  function(response) {
    console.log(response);
  },
  {
    scope: 'user_likes',
    auth_type: 'rerequest'
  }
);

当您这样做时,登录对话框将重新询问被拒绝的权限。对话框看起来非常像关于重新请求权限的部分中的对话框,但是您可以重新请求被拒绝的权限。

所以,使用ASP.NET Identity与Facebook登录的集成,我知道如何传递请求的范围,但如果用户拒绝权限,我需要传递额外的参数auth_type:'rerequest。我这样做?

So, using ASP.NET Identity's integration with Facebook login, I know how to pass in the requested scope, but if the user declines the permission, I need to pass in the extra parameter "auth_type" : 'rerequest." How do I do that?

推荐答案

您首先添加您的自定义FacebookAuthenticationProvider

You first add your custom FacebookAuthenticationProvider

    public class FacebookProvider : FacebookAuthenticationProvider
    {
        public override void ApplyRedirect(FacebookApplyRedirectContext context)
        {
            //To handle rerequest to give some permission
            string authType = string.Empty;
            if (context.Properties.Dictionary.ContainsKey("auth_type"))
            {
                authType = string.Format("&auth_type={0}", context.Properties.Dictionary["auth_type"]);
            }
            //If you have popup loggin add &display=popup
            context.Response.Redirect(string.Format("{0}{1}{2}", context.RedirectUri, "&display=popup", authType));
        }
    }

现在在启动时你需要使用这个提供者

now in the startup you need to use this provider

    var options = new FacebookAuthenticationOptions
    {
        AppId = "appid",
        AppSecret = "secret",

        Provider = new FacebookProvider
        {
            OnAuthenticated = async context =>
            {
                foreach (var x in context.User)
                {
                    if (x.Key == "birthday")
                    {
                        context.Identity.AddClaim(new Claim("dateofbirth", x.Value.ToString()));
                    }
                    else
                    {
                        context.Identity.AddClaim(new Claim(x.Key, x.Value.ToString()));
                    }
                }
                context.Identity.AddClaim(new Claim("fb_accecctoken", context.AccessToken));

                await Task.FromResult(context);
            }

        }
    };
    options.Scope.Add("public_profile");
    options.Scope.Add("email");
    options.Scope.Add("user_birthday");
    options.Scope.Add("user_location");
    app.UseFacebookAuthentication(options);

最后在您的帐户控制器中,您需要在需要时设置auth_type

and finally in your account controller you need to set auth_type when you need

    private const string XsrfKey = "xsrfkey";

    internal class ChallengeResult : HttpUnauthorizedResult
    {
        public ChallengeResult(string provider, string redirectUri)
            : this(provider, redirectUri, null, false)
        {
        }

        public ChallengeResult(string provider, string redirectUri, string userId, bool isRerequest)
        {
            LoginProvider = provider;
            RedirectUri = redirectUri;
            UserId = userId;
            IsRerequest = isRerequest;
        }

        public string LoginProvider { get; set; }
        public string RedirectUri { get; set; }
        public string UserId { get; set; }
        public bool IsRerequest { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
            if (UserId != null)
            {
                properties.Dictionary[XsrfKey] = UserId;
            }
            if (IsRerequest)
            {
                properties.Dictionary["auth_type"] = "rerequest";
            }
            context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
        }
    }

这篇关于ASP.NET身份+ Facebook登录:进入“重新请求”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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