将'hd'参数附加到带有身份标识3的redirectUrl ASP.NET Core 1 [英] Appending 'hd' parameter to redirectUrl ASP.NET Core 1 with Identity 3

查看:107
本文介绍了将'hd'参数附加到带有身份标识3的redirectUrl ASP.NET Core 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有Identity Framework 2的ASP.NET 4中,我可以使用我自己的参数附加redirectUri,例如Google用来将登录名限制为如下所示的域的"hd"参数:

In ASP.NET 4 with the Identity Framework 2 I can append the redirectUri with a parameter of my own, like the 'hd' parameter Google uses to limit login to a domain like this:

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进行相同操作的文档.

But I'm unable to find documentation on how to do the same thing with the new ASP.NET Core 1 with Identity Framework 3.

推荐答案

在GitHub上的源代码的帮助下,我想出了一个可行的解决方案,非常类似于问题中的解决方案:

What I came up with as a working solution, very much like the one in the question, with the help of the source code on GitHub, is the following:

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

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

But is this the correct way to do this, or is there a better way?

这篇关于将'hd'参数附加到带有身份标识3的redirectUrl ASP.NET Core 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆