ASP NET Boilerplate,登录保存在ABPSession中 [英] ASP NET Boilerplate, Login saved in ABPSession

查看:232
本文介绍了ASP NET Boilerplate,登录保存在ABPSession中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp net样板框架的新手,我创建了一个新的mvc项目多页Web应用程序,而没有模块零.

I'm new on the asp net boilerplate framework, and i created a new mvc project multipage web application, without module zero.

我想使用AbpSession类,据我了解,该类在Thread.CurrentPrincipal接管的用户ID内.

I would like to use the AbpSession class that from what I understand has inside the user id that is taken over Thread.CurrentPrincipal.

但是,我不了解登录后如何将用户ID保存在Thread.CurrentPrincipal中.

However, I do not understand how to do after login, to save the user id in the Thread.CurrentPrincipal.

我已经在网络中搜索并找到了几种解决方案,但是在AbpSession类中,用户ID始终为空.

I've searched in the network and found several solutions, but in the AbpSession class the user id is always null.

我发现的最佳解决方案是:

The most optimal solution I found was this:

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样板一起使用,并且我没有找到示例代码.

It's the first time I use principal and identity and despite being documented I did not quite understand how to use them with asp net boilerplate, and I did not find sample codes.

您知道如何告诉我正确的方法或告诉我在哪里可以找到一些功能代码吗?

Do you know how to tell me the right way or tell me where to find some functional codes?

谢谢

推荐答案

开始扩展AbpSession最后一部分清除了思维方式.让我们卷起袖子,在本部分中扩展.AbpSession属性已注入三个基本类中:应用程序服务,AbpController和ABP ApiController.因此,我们需要在域级别扩展AbpSession,这是项目的末尾.核.现在,假设我们需要扩展Email属性.

Start expanding AbpSession The last section has cleared the way of thinking. Let's roll up our sleeves and expand in this section. AbpSession attributes have been injected into three base classes: Application Service, AbpController and ABP ApiController. So we need to extend AbpSession at the domain level, which is the project at the end of. Core. Now suppose we need to extend an Email attribute.

  1. 扩展IAbpSession在结束时找到该项目.核心,添加Extensions文件夹,然后添加从IAbpSession继承的IAbpSession Extension接口:

  1. Extending IAbpSession Locate the project at the end of. Core, add the Extensions folder, and then add the IAbpSession Extension interface inherited from IAbpSession:

命名空间LearningMpaAbp.Extensions{公共接口IAbpSessionExtension:IAbpSession{字符串电子邮件{get;}}}

实现IAbpSession扩展添加基于Claims AbpSession并实现IAbpSession Extension接口的AbpSession Extension类.

Implementing IAbpSession Extension Add the AbpSession Extension class, which is based on Claims AbpSession and implements the IAbpSession Extension interface.

 namespace LearningMpaAbp.Extensions
    {
     public class AbpSessionExtension : ClaimsAbpSession, IAbpSessionExtension, ITransientDependency
        {
            public AbpSessionExtension(
                IPrincipalAccessor principalAccessor,
                IMultiTenancyConfig multiTenancy,
                ITenantResolver tenantResolver,
                IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider) :
                base(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

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;
}

  1. 替换注入的AbbSession属性首先在AbpController中替换注入的ABP会话定位.ApplicationxxxControllerBase:AbpController.CS并注入具有属性的IAbpSession Extension.添加以下代码:

//AbpSession隐藏父类

//AbpSession Hiding Parent Class

public new IAbpSessionExtension AbpSession { get; set; }

在Application Service中替换注入的ABP会话定位.ApplicationxxxAppServiceBase.cs.介绍具有属性的IAbpSession Extension,并添加以下代码:

Replace the injected ABP Session in Application Service Locate. ApplicationxxxAppServiceBase.cs. Introduce IAbpSession Extension with attributes, and add the following code as well:

//AbpSession Hiding Parent Class
public new IAbpSessionExtension AbpSession { get; set; }

在Views AbpRazorPage中更改注入的ABP会话定位.应用程序xxxRazorPage.cs.介绍具有属性的IAbpSession Extension,并添加以下代码:

Chaneg the injected ABP Session in Views AbpRazorPage Locate. ApplicationxxxRazorPage.cs. Introduce IAbpSession Extension with attributes, and add the following code as well:

[RazorInject]
public IAbpSessionExtension AbpSession { get; set; }

这篇关于ASP NET Boilerplate,登录保存在ABPSession中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆