Login registration
This commit is contained in:
parent
716bfb2e05
commit
277dff7d61
22 changed files with 171 additions and 176 deletions
|
|
@ -1,9 +1,6 @@
|
|||
using System.Globalization;
|
||||
using System.Security.Permissions;
|
||||
using Google.Apis.Auth.OAuth2;
|
||||
using Google.Apis.Util.Store;
|
||||
using IdentityServer4;
|
||||
using IdentityServer4.Test;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
|
|
@ -11,7 +8,6 @@ using Microsoft.AspNetCore.Identity;
|
|||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Razor;
|
||||
using Microsoft.AspNetCore.StaticFiles;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
|
@ -25,7 +21,6 @@ using Yavsc.Interface;
|
|||
using Yavsc.Models;
|
||||
using Yavsc.Models.Billing;
|
||||
using Yavsc.Models.Haircut;
|
||||
using Yavsc.Models.Market;
|
||||
using Yavsc.Models.Workflow;
|
||||
using Yavsc.Services;
|
||||
using Yavsc.Settings;
|
||||
|
|
@ -36,8 +31,7 @@ namespace Yavsc.Extensions;
|
|||
|
||||
internal static class HostingExtensions
|
||||
{
|
||||
|
||||
public static IApplicationBuilder ConfigureFileServerApp(this IApplicationBuilder app,
|
||||
public static IApplicationBuilder ConfigureFileServerApp(this IApplicationBuilder app,
|
||||
bool enableDirectoryBrowsing = false)
|
||||
{
|
||||
|
||||
|
|
@ -180,8 +174,9 @@ internal static class HostingExtensions
|
|||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
services
|
||||
.AddIdentityServer(options =>
|
||||
|
||||
|
||||
services.AddIdentityServer(options =>
|
||||
{
|
||||
options.Events.RaiseErrorEvents = true;
|
||||
options.Events.RaiseInformationEvents = true;
|
||||
|
|
@ -194,7 +189,7 @@ internal static class HostingExtensions
|
|||
.AddInMemoryIdentityResources(Config.IdentityResources)
|
||||
.AddInMemoryApiScopes(Config.ApiScopes)
|
||||
.AddInMemoryClients(Config.Clients)
|
||||
.AddDeveloperSigningCredential()
|
||||
.AddAspNetIdentity<ApplicationUser>()
|
||||
;
|
||||
services.AddSession();
|
||||
|
||||
|
|
|
|||
54
src/Yavsc/Extensions/HttpContextExtensions.cs
Normal file
54
src/Yavsc/Extensions/HttpContextExtensions.cs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Access;
|
||||
|
||||
namespace Yavsc.Extensions;
|
||||
|
||||
internal static class HttpContextExtensions
|
||||
{
|
||||
public static async Task SignInAsync(this HttpContext context,
|
||||
ApplicationUser user, RoleManager<IdentityRole> roleManager,
|
||||
bool rememberMe,
|
||||
ApplicationDbContext applicationDbContext)
|
||||
{
|
||||
AuthenticationProperties props = null;
|
||||
if (AccountOptions.AllowRememberLogin && rememberMe)
|
||||
{
|
||||
props = new AuthenticationProperties
|
||||
{
|
||||
IsPersistent = true,
|
||||
ExpiresUtc = DateTimeOffset.UtcNow.Add(AccountOptions.RememberMeLoginDuration),
|
||||
// Parameters =
|
||||
};
|
||||
};
|
||||
|
||||
// roles
|
||||
var roles = applicationDbContext.UserRoles.Where(r => r.UserId == user.Id).ToArray();
|
||||
|
||||
// issue authentication cookie with subject ID and username
|
||||
|
||||
List<Claim> additionalClaims = new List<Claim>();
|
||||
|
||||
foreach (var role in roles)
|
||||
{
|
||||
var idRole = await roleManager.Roles.SingleOrDefaultAsync(i => i.Id == role.RoleId);
|
||||
if (idRole != null)
|
||||
{
|
||||
additionalClaims.Add(new Claim(ClaimTypes.Role, idRole.Name));
|
||||
}
|
||||
}
|
||||
additionalClaims.Add(new Claim(ClaimTypes.Name, user.UserName));
|
||||
var isUser = new IdentityServerUser(user.Id)
|
||||
{
|
||||
DisplayName = user.UserName,
|
||||
AdditionalClaims = additionalClaims.ToArray()
|
||||
};
|
||||
|
||||
await context.SignInAsync(isUser, props);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue