如何在没有身份的情况下将参数传递给ASP.NE Core 2.1中的fb auth回调?

时间:2018-07-04 15:47:15

标签: c# asp.net asp.net-core facebook-oauth

如何将参数传递给Challenge方法,并在Facebook回调后读取它?我想关联社交帐户,并且ID可以是例如用户ID。

帐户控制器示例:

    [Route("accounts/facebook/{id}")]
    public IActionResult Facebook(string id)
    {
        var auth = new AuthenticationProperties { RedirectUri = "/accounts/facebook/callback" };

        return Challenge(auth, "Facebook");
    }

    [Route("accounts/facebook/callback")]
    public void Callback() 
    {
        //need to read custumId
    }

Startup.cs示例:

 public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddAuthentication()
            .AddFacebook(x =>
            {
                x.AppId = "*";
                x.AppSecret = "*";
                x.CallbackPath = "/accounts/facebook/callback";
            });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseDeveloperExceptionPage();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

总结,我需要将社交帐户链接到系统中的现有个人资料。

1 个答案:

答案 0 :(得分:1)

非常简单-您可以在重定向uri中传递所需信息以查询字符串