add Google login
This commit is contained in:
@@ -123,21 +123,43 @@ namespace Kemkas.Web.Areas.Identity.Pages.Account
|
|||||||
{
|
{
|
||||||
return RedirectToPage("./Lockout");
|
return RedirectToPage("./Lockout");
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
// If the user does not have an account, and no email claim, then ask the user to create an account.
|
||||||
// If the user does not have an account, then ask the user to create an account.
|
|
||||||
ReturnUrl = returnUrl;
|
ReturnUrl = returnUrl;
|
||||||
ProviderDisplayName = info.ProviderDisplayName;
|
ProviderDisplayName = info.ProviderDisplayName;
|
||||||
if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
|
if (!info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
|
||||||
{
|
{
|
||||||
|
return Page();
|
||||||
|
}
|
||||||
|
|
||||||
|
// User doesn't have account but external provider has email claim. => Register with claimed email.
|
||||||
Input = new InputModel
|
Input = new InputModel
|
||||||
{
|
{
|
||||||
Email = info.Principal.FindFirstValue(ClaimTypes.Email)
|
Email = info.Principal.FindFirstValue(ClaimTypes.Email)
|
||||||
};
|
};
|
||||||
|
var user = CreateUser();
|
||||||
|
await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
|
||||||
|
await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
|
||||||
|
var registrationResult = await _userManager.CreateAsync(user);
|
||||||
|
if (registrationResult.Succeeded)
|
||||||
|
{
|
||||||
|
registrationResult = await _userManager.AddLoginAsync(user, info);
|
||||||
|
if (registrationResult.Succeeded)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
|
||||||
|
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
|
||||||
|
await _userManager.ConfirmEmailAsync(user, code);
|
||||||
|
await _signInManager.SignInAsync(user, isPersistent: false, info.LoginProvider);
|
||||||
|
return LocalRedirect(returnUrl);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
foreach (var error in registrationResult.Errors)
|
||||||
|
{
|
||||||
|
ModelState.AddModelError(string.Empty, error.Description);
|
||||||
|
}
|
||||||
|
|
||||||
return Page();
|
return Page();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null)
|
public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="8.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
|
||||||
|
|||||||
@@ -33,7 +33,13 @@ builder.Services.AddSingleton<IEmailSender, MailgunEmailSender>();
|
|||||||
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
||||||
.AddEntityFrameworkStores<ApplicationDbContext>();
|
.AddEntityFrameworkStores<ApplicationDbContext>();
|
||||||
|
|
||||||
builder.Services.AddAuthentication();
|
builder.Services.AddAuthentication()
|
||||||
|
.AddGoogle(options =>
|
||||||
|
{
|
||||||
|
var googleAuthNSection = builder.Configuration.GetSection("Authentication:Google");
|
||||||
|
options.ClientId = googleAuthNSection["ClientId"];
|
||||||
|
options.ClientSecret = googleAuthNSection["ClientSecret"];
|
||||||
|
});
|
||||||
|
|
||||||
builder.Services.AddControllersWithViews().AddJsonOptions(options =>
|
builder.Services.AddControllersWithViews().AddJsonOptions(options =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -70,7 +70,13 @@ http {
|
|||||||
proxy_set_header X-Forwarded-Proto "$scheme";
|
proxy_set_header X-Forwarded-Proto "$scheme";
|
||||||
proxy_set_header X-Forwarded-Host "$http_host";
|
proxy_set_header X-Forwarded-Host "$http_host";
|
||||||
proxy_pass https://backend;
|
proxy_pass https://backend;
|
||||||
|
}
|
||||||
|
location /signin-google/ {
|
||||||
proxy_pass_request_headers on;
|
proxy_pass_request_headers on;
|
||||||
|
proxy_set_header X-Forwarded-For "$proxy_add_x_forwarded_for";
|
||||||
|
proxy_set_header X-Forwarded-Proto "$scheme";
|
||||||
|
proxy_set_header X-Forwarded-Host "$http_host";
|
||||||
|
proxy_pass https://backend;
|
||||||
}
|
}
|
||||||
|
|
||||||
location /lib/ {
|
location /lib/ {
|
||||||
|
|||||||
@@ -2,6 +2,12 @@
|
|||||||
"version": 1,
|
"version": 1,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"net8.0": {
|
"net8.0": {
|
||||||
|
"Microsoft.AspNetCore.Authentication.Google": {
|
||||||
|
"type": "Direct",
|
||||||
|
"requested": "[8.0.0, )",
|
||||||
|
"resolved": "8.0.0",
|
||||||
|
"contentHash": "dPE/IiHhBqnJ7WrD72T20+DcHGCaR9F4RmIFap83W1Ts/ze5gBVe4Ii02qJdJzkUniQy5S/v42zpRIgJTPzPqQ=="
|
||||||
|
},
|
||||||
"Microsoft.AspNetCore.DataProtection.EntityFrameworkCore": {
|
"Microsoft.AspNetCore.DataProtection.EntityFrameworkCore": {
|
||||||
"type": "Direct",
|
"type": "Direct",
|
||||||
"requested": "[8.0.0, )",
|
"requested": "[8.0.0, )",
|
||||||
|
|||||||
Reference in New Issue
Block a user