太多的重定向 - Owin外部登录Facebook Asp.Net

时间:2017-09-23 00:22:40

标签: asp.net facebook asp.net-web-api owin

我正在创建一个web-api,我需要使用Facebook记录人们。

我跟随this guide

一旦我向Facebook提供我的凭据,它应该重定向到一个Action,但它会说:"重定向太多。" enter image description here

这是我在 Startup.cs 中得到的:

app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
            FacebookAuthenticationOptions facebookAuthOptions = new FacebookAuthenticationOptions()
            {
                AppId = "myAppId",
                AppSecret = "myAppKey",
                Provider = new FacebookAuthProvider()
            };
            app.UseFacebookAuthentication(facebookAuthOptions);

这是我的 FacebookAuthProvider :class:

public class FacebookAuthProvider : FacebookAuthenticationProvider
    {
        public override Task Authenticated(FacebookAuthenticatedContext context)
        {
            context.Identity.AddClaim(new System.Security.Claims.Claim("ExternalAccessToken", context.AccessToken));
            return Task.FromResult<object>(null);
        }
    }

这是我的 ChallengeResult 类:

public class ChallengeResult : IHttpActionResult
    {
        public string LoginProvider { get; set; }
        public HttpRequestMessage Request { get; set; }

        public ChallengeResult(string loginProvider, ApiController controller)
        {
            LoginProvider = loginProvider;
            Request = controller.Request;
        }
        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            Request.GetOwinContext().Authentication.Challenge(LoginProvider);

            HttpResponseMessage response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
            response.RequestMessage = Request;
            return Task.FromResult<HttpResponseMessage>(response);
        }
    }

这是我在用户登录后用来从Facebook获取令牌的控制器:

[HttpGet]
        [OverrideAuthentication]
        [HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
        [AllowAnonymous]
        //[Route("ExternalLogin", Name = "ExternalLogin")]
        public IHttpActionResult GetExternalLogin(string provider)
        {
            string redirectUri = string.Empty;
            AppUserManager manager = new AppUserManager(new AppUserStore(new AppContext()));

            if (!User.Identity.IsAuthenticated)
            {
                return new ChallengeResult(provider, this);
            }

            ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

            UserLoginInfo loginInfo = new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey);

            IdentityUser user = manager.Find(loginInfo);

            bool hasRegistered = user != null;

            ValidateRedirectUri(this.Request, ref redirectUri);

            redirectUri = String.Format("{0}#external_access_token={1}&provider={2}&haslocalaccount={3}&external_user_name={4}",
                                            redirectUri,
                                            externalLogin.AccessToken,
                                            externalLogin.LoginProvider,
                                            hasRegistered.ToString(),
                                            externalLogin.UserName);

            return Redirect(redirectUri);
        }

我真正好奇的一件事是,如果我取消注释这一行:

[Route("ExternalLogin", Name = "ExternalLogin")]

尝试使用新路由访问该控制器,它说User(GetExternalLogin中的那个&#39; s)是null

这是我用来测试的链接: http://localhost:62887/api/ExternalAuth/GetExternalLogin?provider=Facebook&redirect_uri=http://localhost:62887/api/ExternalAuth/LoggedIn

用户成功登录后,这是他应该重定向的操作:

[HttpGet]
        public IHttpActionResult LoggedIn()
        {
            return Ok(new { Message = "You've been successfully logged in! :)" });
        }

1 个答案:

答案 0 :(得分:0)

我会自杀,我终于开始工作了。我只需要将NuGet包从2.1更新到3.1 ...&gt;:/