最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core - .NET OAuth2 Sign-on Google server-side & Azure Sign-on - Stack Overflow

programmeradmin0浏览0评论

I am having trouble finding the problem with my authentication services. I want to be able to log in with google OAuth 2.0 as well as Azure. For now only azure works. My code goes as followed:

builder.Services
.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));

builder.Services
.AddAuthentication()
.AddGoogle(options =
{
    IConfigurationSection googleAuthNSection = 
    builder.Configuration.GetSection("Authentication:Google");
    options.ClientId = googleAuthNSection["ClientId"];
    options.ClientSecret = googleAuthNSection["ClientSecret"];
});

With this sign in button:

<MudButton Href="/signin-google" Variant="Variant.Filled" Color="Color.Primary">Log in with Google</MudButton>

The problem is that the google sign-on always results in this and I can't find the problem:

Error google login redirect

I have tried editing the redirect uris in google cloud, but that also didn't work. Redirect URIS

I think it is a cookie issue, but I don't know how to fix it so both login options would work.

I am having trouble finding the problem with my authentication services. I want to be able to log in with google OAuth 2.0 as well as Azure. For now only azure works. My code goes as followed:

builder.Services
.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));

builder.Services
.AddAuthentication()
.AddGoogle(options =
{
    IConfigurationSection googleAuthNSection = 
    builder.Configuration.GetSection("Authentication:Google");
    options.ClientId = googleAuthNSection["ClientId"];
    options.ClientSecret = googleAuthNSection["ClientSecret"];
});

With this sign in button:

<MudButton Href="/signin-google" Variant="Variant.Filled" Color="Color.Primary">Log in with Google</MudButton>

The problem is that the google sign-on always results in this and I can't find the problem:

Error google login redirect

I have tried editing the redirect uris in google cloud, but that also didn't work. Redirect URIS

I think it is a cookie issue, but I don't know how to fix it so both login options would work.

Share Improve this question edited Feb 3 at 7:59 Qiang Fu 9,4171 gold badge6 silver badges16 bronze badges asked Jan 31 at 10:43 Thomas VerbruggenThomas Verbruggen 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

HttpContext can only have one user.So you could login either Azure or Google, but not at same time. Since they both implicitly use default cookie which named .AspNetCore.Cookies, you need to set different cookie for one of them.

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));

builder.Services.AddAuthentication()
.AddCookie("GoogleAuthCookieScheme", options =>
{
    options.Cookie.Name = "MyGoogleAuth";
})
.AddGoogle(options =>
{
    IConfigurationSection googleAuthNSection = builder.Configuration.GetSection("Authentication:Google");
    options.ClientId = googleAuthNSection["ClientId"];
    options.ClientSecret = googleAuthNSection["ClientSecret"];
    options.SignInScheme = "GoogleAuthCookieScheme";
});
发布评论

评论列表(0)

  1. 暂无评论