-
Notifications
You must be signed in to change notification settings - Fork 747
/
IdentityPackBase.cs
130 lines (113 loc) · 4.88 KB
/
IdentityPackBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// -----------------------------------------------------------------------
// <copyright file="IdentityPackBase.cs" company="OSharp开源团队">
// Copyright (c) 2014-2018 OSharp. All rights reserved.
// </copyright>
// <site>http://www.osharp.org</site>
// <last-editor>郭明锋</last-editor>
// <last-date>2018-06-23 15:27</last-date>
// -----------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Reflection;
using System.Security.Claims;
using System.Security.Principal;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OSharp.Core.Packs;
using OSharp.Data;
using OSharp.EventBuses;
using OSharp.Identity.Entities;
using OSharp.Identity.Events;
namespace OSharp.Identity;
/// <summary>
/// 身份标识模块基类
/// </summary>
[Description("身份标识模块")]
[DependsOnPacks(typeof(EventBusPack))]
public abstract class IdentityPackBase<TUserStore, TRoleStore, TUser, TUserKey, TUserClaim, TUserClaimKey, TRole, TRoleKey> : OsharpPack
where TUserStore : class, IUserStore<TUser>
where TRoleStore : class, IRoleStore<TRole>
where TUser : UserBase<TUserKey>
where TUserKey : IEquatable<TUserKey>
where TUserClaim : UserClaimBase<TUserClaimKey, TUserKey>
where TUserClaimKey : IEquatable<TUserClaimKey>
where TRole : RoleBase<TRoleKey>
where TRoleKey : IEquatable<TRoleKey>
{
/// <summary>
/// 获取 模块级别
/// </summary>
public override PackLevel Level => PackLevel.Application;
/// <summary>
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,同一级别内部再按此顺序启动,
/// 级别默认为0,表示无依赖,需要在同级别有依赖顺序的时候,再重写为>0的顺序值
/// </summary>
public override int Order => 0;
/// <summary>
/// 将模块服务添加到依赖注入服务容器中
/// </summary>
/// <param name="services">依赖注入服务容器</param>
/// <returns></returns>
public override IServiceCollection AddServices(IServiceCollection services)
{
services.AddScoped<IUserStore<TUser>, TUserStore>();
services.AddScoped<IRoleStore<TRole>, TRoleStore>();
services.AddEventHandler<OnlineUserCacheRemoveEventHandler>();
//在线用户缓存
services.TryAddScoped<IOnlineUserProvider, OnlineUserProvider<TUser, TUserKey, TUserClaim, TUserClaimKey, TRole, TRoleKey>>();
//替换 IPrincipal,设置用户主键类型,用以在Repository进行审计时注入正确用户主键类型
services.Replace(new ServiceDescriptor(typeof(IPrincipal),
provider =>
{
IHttpContextAccessor accessor = provider.GetService<IHttpContextAccessor>();
ClaimsPrincipal principal = accessor?.HttpContext?.User;
if (principal is { Identity: ClaimsIdentity identity })
{
PropertyInfo property = typeof(TUser).GetProperty("Id");
if (property != null && !identity.HasClaim(m => m.Type == OsharpConstants.UserIdTypeName))
{
identity.AddClaim(new Claim(OsharpConstants.UserIdTypeName, property.PropertyType.FullName));
}
}
return principal;
},
ServiceLifetime.Transient));
Action<IdentityOptions> identityOptionsAction = IdentityOptionsAction();
IdentityBuilder builder = services.AddIdentityCore<TUser>(identityOptionsAction)
.AddRoles<TRole>().AddSignInManager();
services.Replace(new ServiceDescriptor(typeof(IdentityErrorDescriber), typeof(IdentityErrorDescriberZhHans), ServiceLifetime.Scoped));
OnIdentityBuild(builder);
return services;
}
/// <summary>
/// 重写以实现<see cref="IdentityOptions"/>的配置
/// </summary>
/// <returns></returns>
protected virtual Action<IdentityOptions> IdentityOptionsAction()
{
return options =>
{
//登录
options.SignIn.RequireConfirmedEmail = false;
//密码
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
//用户
options.User.RequireUniqueEmail = false;
//锁定
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
};
}
/// <summary>
/// 重写以实现 AddIdentity 之后的构建逻辑
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
protected virtual IdentityBuilder OnIdentityBuild(IdentityBuilder builder)
{
return builder.AddDefaultTokenProviders();
}
}