You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.8 KiB
C#

9 months ago
using EntrustSettle.Common;
using EntrustSettle.Common.Caches;
using EntrustSettle.Common.Const;
using EntrustSettle.IRepository.Base;
using EntrustSettle.Model.Models.DJY;
using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;
namespace EntrustSettle.Extensions.Middlewares
{
public class SetAppUserMiddleware
{
private readonly RequestDelegate _next;
private readonly ICaching _caching;
public SetAppUserMiddleware(RequestDelegate next, IBaseRepository<User> repository, ICaching caching)
{
_next = next;
_caching = caching;
}
public async Task Invoke(HttpContext context)
{
var userId = App.User?.ID;
if (!string.IsNullOrEmpty(userId) && !App.User.IsSetUser)
{
/*
ClaimsIdentity identity = new ClaimsIdentity("AuthenticationTypes.Federation");
identity.AddClaim(new Claim(type: ClaimConst.CLAINM_USERID, value: user.Id.ToString()));
identity.AddClaim(new Claim(type: ClaimConst.CLAINM_ACCOUNT, value: user.Account));
identity.AddClaim(new Claim(type: ClaimConst.CLAINM_NAME, value: user.Name));
identity.AddClaim(new Claim(type: ClaimConst.CLAINM_SUPERADMIN, value: ((int)user.AdminType).ToString()));
identity.AddClaim(new Claim(type: ClaimConst.CLAINM_TENANT_TYPE, value: tenant.TenantType.ToString()));
identity.AddClaim(new Claim(type: ClaimConst.TENANT_ID, value: tenant.Id.ToString()));
identity.AddClaim(new Claim(type: ClaimConst.TENANT_NAME, value: tenant.Name));
identity.AddClaim(new Claim(type: ClaimConst.DjyCompanyId, value: tenant.CompId == null ? string.Empty : tenant.CompId));
identity.AddClaim(new Claim(type: ClaimConst.DjyUserId, value: user.DjyUserId));
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);
httpContext.User = claimsPrincipal;
*/
9 months ago
var user = _caching.Get<User>(CacheConst.KeyUserInfoCache + userId);
if (user != null)
{
App.User.SetUser(user);
}
else
{
var resp = App.GetService<IBaseRepository<User>>();
user = await resp.QueryFirst(x => x.GID == App.User.ID && !x.IsLeave);
if (user != null)
{
_caching.Set(CacheConst.KeyUserInfoCache + userId, user, TimeSpan.FromMinutes(AppSettings.app("Startup", "UserInfoCacheTime").ObjToInt()));
App.User.SetUser(user);
}
}
}
await _next(context);
}
}
}