【问题标题】:Appending 'hd' parameter to redirectUrl ASP.NET Core 1 with Identity 3将“hd”参数附加到具有标识 3 的 redirectUrl ASP.NET Core 1
【发布时间】:2016-01-21 16:50:23
【问题描述】:

在带有 Identity Framework 2 的 ASP.NET 4 中,我可以在 redirectUri 中附加我自己的参数,例如 Google 用来限制登录到这样的域的“hd”参数:

var googleAuthOptions = new GoogleOAuth2AuthenticationOptions
{
    ClientId = "redacted",
    ClientSecret = "redacted",
    Provider = new CustomGoogleProvider
    {
        OnApplyRedirect = context =>
        {
            var redirect = context.RedirectUri;
            redirect += "&hd=contoso.com";
            context.Response.Redirect(redirect);
        }
    }
};

app.UseGoogleAuthentication(googleAuthOptions);

但我无法找到有关如何使用带有 Identity Framework 3 的新 ASP.NET Core 1 执行相同操作的文档。

【问题讨论】:

    标签: c# asp.net asp.net-identity google-oauth asp.net-core


    【解决方案1】:

    借助 GitHub 上的源代码,我想出了一个可行的解决方案,与问题中的解决方案非常相似,如下所示:

    app.UseGoogleAuthentication(options => {
        options.ClientId = Configuration["Authentication:Google:ClientId"];
        options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
    
        options.Events = new OAuthEvents()
        {
            OnRedirectToAuthorizationEndpoint = context =>
            {
                context.Response.Redirect(context.RedirectUri + "&hd=contoso.com");
                return Task.FromResult(0);
            }
        };
    });
    

    但这是正确的方法吗,还是有更好的方法?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-14
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 2016-11-26
      • 2017-02-12
      • 1970-01-01
      相关资源
      最近更新 更多