Warm tip: This article is reproduced from serverfault.com, please click

.net-用长颈鹿登录Facebook

(.net - Facebook login with Giraffe)

发布于 2020-11-23 18:26:29

我正在尝试将我的MVC应用程序转换为长颈鹿,但最后一位老板是:Facebook登录。

除了挑战之外,我已经能够使所有部分发挥作用:

        public IActionResult ExternalLogin(string returnUrl = null)
        {
            var properties = _signInManager.ConfigureExternalAuthenticationProperties("Facebook", $"/mycallbackurl?returnUrl={returnUrl}");
            return new ChallengeResult("Facebook",   properties);
        }

如何在长颈鹿中做到这一点?

当我简单地返回时challenge "Facebook",流程工作正常,除非返回到回调端点

let! info = signInManager.GetExternalLoginInfoAsync() info为空。

控制台甚至说

info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[10]
      AuthenticationScheme: Identity.External signed in

我如何掌握此事件以使用登录我的用户signInManager.SignInAsync

Startup.cs


 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(o =>
                    {
                        o.Cookie.Expiration = TimeSpan.FromDays(16);
                        o.Cookie.Name = "_myt";
                        o.SlidingExpiration = true;
                    }

                )
                .AddFacebook(o =>
                {
                    o.AppId = Configuration["Authentication:Facebook:AppId"];
                    o.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
                });
Questioner
severin
Viewed
11
severin 2020-11-30 16:51:43

signInManager.GetExternalLoginInfoAsync() 依靠发出包含回调URL的挑战,而Giraffe的挑战则不这样做。

一个解决方案是自己动手challenge

let externalLogin : HttpHandler =
    fun (next : HttpFunc) (ctx : HttpContext) ->
        task {
            let provider = "Facebook"

            let returnUrl =
                (ctx.TryGetQueryStringValue "returnUrl"
                 |> Option.map (sprintf "?returnurl=%s")
                 |> Option.defaultValue "")

            let items = // This must be a mutable dictionary to work
                System.Collections.Generic.Dictionary<string, string>()
            
            items.Add("LoginProvider", provider)
                
            let props = AuthenticationProperties(items, RedirectUri = (sprintf "/myCallbackEndpointWhereILogTheUserIn%s" returnUrl))
            
            do! ctx.ChallengeAsync(provider, props)
            return! next ctx
        }