c# - 将 'hd' 参数附加到 redirectUrl ASP.NET Core 1 with Identity 3

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

在带有 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);

但我无法找到有关如何使用带有身份框架 3 的新 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);
        }
    };
});

但这是执行此操作的正确方法,还是有更好的方法?

关于c# - 将 'hd' 参数附加到 redirectUrl ASP.NET Core 1 with Identity 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34067530/

相关文章:

c# - Xamarin.android如何添加snackbar回调

asp.net - 为什么 HttpContext.Current.Session 在 Global.asax 中为空?

javascript - AjaxFileUpload cancelUpload() 不起作用

c# - ASP.Net MVC 3 中单独项目的数据访问层和业务逻辑层

c# - 基于字符串选择多个属性的值

c# - HiddenField 的值未更新

angular - 使用 Angular 搭建 Core 3 项目时,在 JS 中保持登录/注册,而不是 Razor View

asp.net-mvc - 使用 Controller 的单页应用程序 - 如何使用 ASP.NET Identity 保证安全?

c# - 使用 SendGrid v3 的身份将交易模板作为确认电子邮件发送

C# 为什么 Array.IndexOf 返回 -1