Google OAuth.

This commit is contained in:
Haiping Chen 2024-02-15 19:47:02 -06:00
parent 5f80b5e802
commit c75dcb54db
4 changed files with 26 additions and 12 deletions

View file

@ -18,16 +18,19 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.2" />
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="8.0.2" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="6.0.27" />
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="6.0.15" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.25" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.26" />

View file

@ -80,6 +80,7 @@ public static class BotSharpOpenApiExtensions
};
});
// GitHub OAuth
if (!string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientSecret"]))
{
builder = builder.AddGitHub(options =>
@ -90,6 +91,16 @@ public static class BotSharpOpenApiExtensions
});
}
// Google Identiy OAuth
if (!string.IsNullOrWhiteSpace(config["OAuth:Google:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:Google:ClientSecret"]))
{
builder = builder.AddGoogle(options =>
{
options.ClientId = config["OAuth:Google:ClientId"];
options.ClientSecret = config["OAuth:Google:ClientSecret"];
});
}
// Add services to the container.
services.AddControllers()
.AddJsonOptions(options =>

View file

@ -1,4 +1,3 @@
using AspNet.Security.OAuth.GitHub;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using System.ComponentModel.DataAnnotations;
@ -71,18 +70,15 @@ public class UserController : ControllerBase
var identiy = _services.GetRequiredService<IUserIdentity>();
var accessor = _services.GetRequiredService<IHttpContextAccessor>();
var claims = accessor.HttpContext.User.Claims;
if (claims.Any(x => x.Type == GitHubAuthenticationConstants.Claims.Name))
user = await _userService.CreateUser(new User
{
user = await _userService.CreateUser(new User
{
Email = identiy.Email,
UserName = identiy.UserName,
FirstName = identiy.FirstName,
LastName = identiy.LastName,
Source = "GitHub",
ExternalId = identiy.Id,
});
}
Email = identiy.Email,
UserName = identiy.UserName,
FirstName = identiy.FirstName,
LastName = identiy.LastName,
Source = claims.First().Issuer,
ExternalId = identiy.Id,
});
}
return UserViewModel.FromUser(user);
}

View file

@ -17,6 +17,10 @@
"GitHub": {
"ClientId": "",
"ClientSecret": ""
},
"Google": {
"ClientId": "",
"ClientSecret": ""
}
},