簡體   English   中英

使用長頸鹿登錄 Facebook

[英]Facebook login with Giraffe

我正在嘗試將我的 MVC 應用程序轉換為 Giraffe,但有一個最終的老板: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() let! info = signInManager.GetExternalLoginInfoAsync()信息為空。

控制台甚至說

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

如何使用signInManager.SignInAsync獲取此事件以登錄我的用戶?

啟動文件


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

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
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM