我是asp网络样板框架的新手,我创建了一个新的mvc项目多页面Web应用程序,没有模块零。
我想使用AbpSession类,根据我的理解,它包含在通过Thread.CurrentPrincipal获取的用户ID中。
但是,我不明白登录后如何做,将用户ID保存在Thread.CurrentPrincipal中。
我在网络中搜索并找到了几个解决方案,但在AbpSession类中,用户ID始终为null。
我找到的最佳解决方案是:
IList<Claim> claimCollection = new List<Claim>
{
new Claim(AbpClaimTypes.UserId, "5")
};
ClaimsIdentity claimsIdentity = new ClaimsIdentity(claimCollection);
var principal = new ClaimsPrincipal(claimsIdentity);
Thread.CurrentPrincipal = principal;
这是我第一次使用主体和身份,尽管有记录,但我不太明白如何在asp net样板中使用它们,我没有找到样本代码。
你知道如何告诉我正确的方法或告诉我在哪里找到一些功能代码吗?
由于
答案 0 :(得分:0)
尽管问题很普遍,但我想与大家分享一些关于如何在ASP.NET Core中向AbpSession添加自定义字段的代码。
<强> MyAppSession.cs 强>
//Define your own session and add your custom field to it
//Then, you can inject MyAppSession and use it's new property in your project.
public class MyAppSession : ClaimsAbpSession, ITransientDependency
{
public MyAppSession(
IPrincipalAccessor principalAccessor,
IMultiTenancyConfig multiTenancy,
ITenantResolver tenantResolver,
IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider) :
base(principalAccessor, multiTenancy, tenantResolver, sessionOverrideScopeProvider)
{
}
public string UserEmail
{
get
{
var userEmailClaim = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == "Application_UserEmail");
if (string.IsNullOrEmpty(userEmailClaim?.Value))
{
return null;
}
return userEmailClaim.Value;
}
}
}
<强> UserClaimsPrincipalFactory.cs 强>
//Override CreateAsync method to add your custom claim
public override async Task<ClaimsPrincipal> CreateAsync(User user)
{
var claim = await base.CreateAsync(user);
claim.Identities.First().AddClaim(new Claim("Application_UserEmail", user.EmailAddress));
return claim;
}
答案 1 :(得分:0)
开始扩展AbpSession 最后一部分清除了思维方式。让我们卷起袖子,在本部分中扩展。 AbpSession属性已注入三个基本类中:应用程序服务,AbpController和ABP ApiController。 因此,我们需要在域级别扩展AbpSession,这是项目的末尾。核心。 现在假设我们需要扩展Email属性。
扩展IAbpSession 在结束时找到该项目。核心,添加Extensions文件夹,然后添加从IAbpSession继承的IAbpSession Extension接口:
命名空间LearningMpaAbp.Extensions { 公共接口IAbpSessionExtension:IAbpSession { 字符串电子邮件{get; } } }
实现IAbpSession扩展 添加基于Claims AbpSession并实现IAbpSession Extension接口的AbpSession Extension类。
命名空间LearningMpaAbp.Extensions { 公共类AbpSessionExtension:ClaimsAbpSession,IAbpSessionExtension,ITransientDependency { 公共AbpSessionExtension( IPrincipalAccessorPrincipalAccessor, IMultiTenancyConfig multiTenancy, ITenantResolver tenantResolver, IAmbientScopeProvider sessionOverrideScopeProvider): 基本(principalAccessor,multiTenancy,tenantResolver,sessionOverrideScopeProvider) {
}
public string Email => GetClaimValue(ClaimTypes.Email);
private string GetClaimValue(string claimType)
{
var claimsPrincipal = PrincipalAccessor.Principal;
var claim = claimsPrincipal?.Claims.FirstOrDefault(c => c.Type == claimType);
if (string.IsNullOrEmpty(claim?.Value))
return null;
return claim.Value;
}
}
}
UserClaimsPrincipalFactory.cs
//Override CreateAsync method to add your custom claim
public override async Task<ClaimsPrincipal> CreateAsync(User user)
{
var claim = await base.CreateAsync(user);
claim.Identities.First().AddClaim(new Claim(ClaimTypes.Email, user.EmailAddress));
return claim;
}
替换注入的AbbSession属性 首先在AbpController中替换注入的ABP会话 定位。 Web控制器xxxController基础。 CS并注入具有属性的IAbpSession Extension。添加以下代码:
// AbpSession隐藏父类 公开新的IAbpSessionExtension AbpSession {get;组; }
在Application Service中替换注入的ABP会话 定位。 ApplicationxxxAppServiceBase.cs。介绍具有属性的IAbpSession Extension,并添加以下代码:
//AbpSession Hiding Parent Class
public new IAbpSessionExtension AbpSession { get; set; }