返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期

上一节我们讲到登录逻辑,我做的登录逻辑很简单的,我们来看一下abp module-zero里面的登录代码.

 #region Login / Logoutpublic ActionResult Login(string returnUrl = ""){if (string.IsNullOrWhiteSpace(returnUrl)){returnUrl = Request.ApplicationPath;}return View(new LoginFormViewModel{ReturnUrl = returnUrl,IsMultiTenancyEnabled = _multiTenancyConfig.IsEnabled});}[HttpPost]public async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = ""){CheckModelState();var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress,loginModel.Password,loginModel.TenancyName);await SignInAsync(loginResult.User, loginResult.Identity, loginModel.RememberMe);if (string.IsNullOrWhiteSpace(returnUrl)){returnUrl = Request.ApplicationPath;}if (!string.IsNullOrWhiteSpace(returnUrlHash)){returnUrl = returnUrl + returnUrlHash;}return Json(new AjaxResponse { TargetUrl = returnUrl });}private async Task<AbpLoginResult<Tenant, User>> GetLoginResultAsync(string usernameOrEmailAddress, string password, string tenancyName){try{var loginResult = await _logInManager.LoginAsync(usernameOrEmailAddress, password, tenancyName);switch (loginResult.Result){case AbpLoginResultType.Success:return loginResult;default:throw CreateExceptionForFailedLoginAttempt(loginResult.Result, usernameOrEmailAddress, tenancyName);}}catch (Exception e){Console.WriteLine(e);throw;}}private async Task SignInAsync(User user, ClaimsIdentity identity = null, bool rememberMe = false){if (identity == null){identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);}AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, identity);}private Exception CreateExceptionForFailedLoginAttempt(AbpLoginResultType result, string usernameOrEmailAddress, string tenancyName){switch (result){case AbpLoginResultType.Success:return new ApplicationException("Don't call this method with a success result!");case AbpLoginResultType.InvalidUserNameOrEmailAddress:case AbpLoginResultType.InvalidPassword:return new UserFriendlyException(L("LoginFailed"), L("InvalidUserNameOrPassword"));case AbpLoginResultType.InvalidTenancyName:return new UserFriendlyException(L("LoginFailed"), L("ThereIsNoTenantDefinedWithName{0}", tenancyName));case AbpLoginResultType.TenantIsNotActive:return new UserFriendlyException(L("LoginFailed"), L("TenantIsNotActive", tenancyName));case AbpLoginResultType.UserIsNotActive:return new UserFriendlyException(L("LoginFailed"), L("UserIsNotActiveAndCanNotLogin", usernameOrEmailAddress));case AbpLoginResultType.UserEmailIsNotConfirmed:return new UserFriendlyException(L("LoginFailed"), "UserEmailIsNotConfirmedAndCanNotLogin");case AbpLoginResultType.LockedOut:return new UserFriendlyException(L("LoginFailed"), L("UserLockedOutMessage"));default: //Can not fall to default actually. But other result types can be added in the future and we may forget to handle itLogger.Warn("Unhandled login fail reason: " + result);return new UserFriendlyException(L("LoginFailed"));}}public ActionResult Logout(){AuthenticationManager.SignOut();return RedirectToAction("Login");}#endregion

由于abp涉及到租户和身份验证的问题,所以登录有点繁琐.分析发现主要包括以下几个步骤:

1、 GetLoginResultAsync --> loginManager.LoginAsync --> userManager.CreateIdentityAsync:不要以为调用了LoginAsync就以为是登录,其实这是伪登录。主要根据用户名密码去核对用户信息,构造User对象返回,然后再根据User对象的身份信息去构造身份证(CliamsIdentity)。
2、SignInAsync --> AuthenticationManager.SignOut
-->AuthenticationManager.SignIn

AuthenticationManager(认证管理员),负责真正的登入登出。SignIn的时候将第一步构造的身份证(CliamsIdentity)交给证件所有者(ClaimsPrincipal)。   
登录完成之后,我们通常会有一个记住用户名密码的功能,有人就会想到abp中的AbpSession.单其实AbpSession不是单纯意义上的Session,比如AbpSession里面的Userid就是通过以下方式获得的.
((ClaimsPrincipal)Thread.CurrentPrincipal).Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);

需要获取会话信息则必须实现IAbpSession接口。虽然你可以用自己的方式去实现它(IAbpSession),但是它在module-zero项目中已经有了完整的实现。IAbpSession包含还有其他信息.

 //// 摘要://     Defines some session information that can be useful for applications.public interface IAbpSession{//// 摘要://     TenantId of the impersonator. This is filled if a user with Abp.Runtime.Session.IAbpSession.ImpersonatorUserId//     performing actions behalf of the Abp.Runtime.Session.IAbpSession.UserId.int? ImpersonatorTenantId { get; }//// 摘要://     UserId of the impersonator. This is filled if a user is performing actions behalf//     of the Abp.Runtime.Session.IAbpSession.UserId.long? ImpersonatorUserId { get; }//// 摘要://     Gets current multi-tenancy side.MultiTenancySides MultiTenancySide { get; }//// 摘要://     Gets current TenantId or null. This TenantId should be the TenantId of the Abp.Runtime.Session.IAbpSession.UserId.//     It can be null if given Abp.Runtime.Session.IAbpSession.UserId is a host user//     or no user logged in.int? TenantId { get; }//// 摘要://     Gets current UserId or null. It can be null if no user logged in.long? UserId { get; }//// 摘要://     Used to change Abp.Runtime.Session.IAbpSession.TenantId and Abp.Runtime.Session.IAbpSession.UserId//     for a limited scope.//// 参数://   tenantId:////   userId:IDisposable Use(int? tenantId, long? userId);

AbpSession定义的一些关键属性:

1.UserId: 当前用户的标识ID,如果没有当前用户则为null.如果需要授权访问则它不可能为空。

2.TenantId: 当前租户的标识ID,如果没有当前租户则为null。

3.MultiTenancySide: 可能是Host或Tenant。

UserId和TenantId是可以为null的。当然也提供了不为空时获取数据的 GetUserId()和GetTenantId() 方法 。当你确定有当前用户时,你可以使用GetUserId()方法。如果当前用户为空,使用该方法则会抛出一个异常。GetTenantId()的使用方式和GetUserId()类似。

IAbpSession通常是以属性注入的方式存在于需要它的类中,不需要获取会话信息的类中则不需要它。如果我们使用属性注入方式,我们可以用 
NullAbpSession.Instance作为默认值来初始化它(IAbpSession)

    public IAbpSession AbpSession { get; set; }private readonly IUserService _iUsersService;public AccountController(IUserService iUsersService){_iUsersService = iUsersService;AbpSession = NullAbpSession.Instance;}// GET: Accountpublic ActionResult Index(){var currentUserId = AbpSession.UserId;return View(); }

由于授权是应用层的任务,因此我们应该在应用层和应用层的上一层使用IAbpSession(我们不在领域层使用IAbpSession是很正常的)。

ApplicationServiceAbpController 和 AbpApiController 这3个基类已经注入了AbpSession属性,因此在Application Service的实例方法中,能直接使用AbpSession属性。

ABP框架中的AbpSession, 并没有使用到System.Web.HttpSessionStateBase, 而是自己定义了一个Abp.Runtime.Session.IAbpSession接口, 并在Zero模块中通过AspNet.Identity组件实现了AbpSession对象的存值、取值。 所以即使Web服务重启,也不会丢失Session状态。在我们自己的项目中, Session对象只有UserId、TenantId、MultiTenancySide这几个属性是不够用的,可以自己扩充了几个属性和方法,使用起来非常方便。

首先我们定义IAbpSession扩展类获取扩展属性,通过扩展类,我们不需要做其他额外的更改,即可通过ApplicationService, AbpController 和 AbpApiController 这3个基类已经注入的AbpSession属性调用GetUserName()来获取扩展的Name属性。

接口代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace JCmsErp.AbpSessionExtension
{public interface IAbpSessionExtension{string UserName { get; }}
}

实现代码:

using Abp.Configuration.Startup;
using Abp.MultiTenancy;
using Abp.Runtime;
using Abp.Runtime.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;namespace JCmsErp.AbpSessionExtension
{public class AbpSessionExtension : ClaimsAbpSession, IAbpSessionExtension{public AbpSessionExtension(IPrincipalAccessor principalAccessor, IMultiTenancyConfig multiTenancy, ITenantResolver tenantResolver, IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider): base(principalAccessor, multiTenancy, tenantResolver, sessionOverrideScopeProvider){}public string UserName => GetUserName(ClaimTypes.Name);private string GetUserName(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;}}
}

然后在登录逻辑中加入以下代码:

  //添加身份信息,以便在AbpSession中使用
identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));

就这样,我们在ApplicationService, AbpController 和 AbpApiController任何地方注入IAbpSession,然后AbpSession.Name就能获取到我们登录时候添加的信息.

二,abp的错误机制

如果登录过程中出错怎么办,报错了ABP怎么反应,我们来看一下abp的错误机制.在web应用中,异常通常在MVC Controller actions和Web API Controller actions中处理。当异常发生时,应用程序的用户以某种方式被告知错误的相关信息及原因。果错误在正常的HTTP请求时发生,将会显示一个异常页。如果在AJAX请求中发生错误,服务器发送错误信息到客户端,然后客户端处理错误并显示给用户。在所有的Web请求中处理异常是件乏味且重复的工作。ABP自动化完成异常处理,几乎从不需要显示的处理任何异常。ABP处理所有的异常、记录异常并返回合适、格式化的响应到客户端。在客户端处理这些响应并将错误信息显示给用户。

异常显示,首先我们在ActionResult 随便添加一个异常信息,调试一下看一下结果

   public ActionResult Index(){// return View();throw new Exception("登录密码错误或用户不存在或用户被禁用。");}

当然,这个异常可能由另一个方法抛出,而这个方法的调用在这个action里。ABP处理这个异常、记录它并显示'Error.cshtml'视图。你可以自定义这个视图来显示错误。一个示例错误视图(在ABP模板中的默认错误视图):

BP对用户隐藏了异常的细节并显示了一个标准(本地化的)的错误信息,除非你显示的抛出一个UserFriendlyException,UserFriendlyException UserFriendlyException是一个特殊类型的异常,它直接显示给用户。参见下面的示例:

    // GET: Accountpublic ActionResult Index(){// return View();throw new Abp.UI.UserFriendlyException("登录密码错误或用户不存在或用户被禁用。");}

浏览器结果:

所以,如果你想显示一个特定的错误信息给用户,那就抛出一个UserFriedlyException(或者一个继承自这个类的异常)。

当然如果是ajax请求里面出错,message API处理JSON对象并显示错误信息给用户。前端应该有相应的错误处理.

  返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期

转载于:https://www.cnblogs.com/anyushengcms/p/7261815.html

ABP+AdminLTE+Bootstrap Table权限管理系统第八节--ABP错误机制及AbpSession相关相关推荐

  1. ABP+AdminLTE+Bootstrap Table权限管理系统一期

      初衷    学而时习之,不亦说乎,温顾温知新,可以为师矣.           看懂远不如动手去做,动手做才能发现很多自己不懂的问题,不断的反思和总结,"乐于分享是一种境界的突破&quo ...

  2. ABP+AdminLTE+Bootstrap Table权限管理系统第五节--WBEAPI及SwaggerUI

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 一,Web API ABP的动态WebApi实现了直接对服务层的调用(其实病没有跨过ApiController,只 ...

  3. ABP+AdminLTE+Bootstrap Table权限管理系统第九节--AdminLTE引入及模板页和布局和菜单...

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 AdminLTE AdminLTE 官网地址:https://adminlte.io/themes/AdminLT ...

  4. ABP+AdminLTE+Bootstrap Table权限管理系统第二节--在ABP的基础做数据库脚本处理

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 第一点,上一篇文章中我们讲到codefirst中一些问题包括如图,codefirst在每次执行命令的时候会生成新的 ...

  5. ABP+AdminLTE+Bootstrap Table权限管理系统第四节--仓储,服务,服务接口及依赖注入

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 AbpModule 在ABP框架中,仓储,服务,这块算是最为重要一块之一了.ABP框架提供了创建和组装模块的基础, ...

  6. ABP+AdminLTE+Bootstrap Table权限管理系统第十一节--bootstrap table之用户管理列表

    ABP+AdminLTE+Bootstrap Table权限管理系统一期 Github:https://github.com/Jimmey-Jiang/ABP-ASP.NET-Boilerplate- ...

  7. ABP module-zero +AdminLTE+Bootstrap Table+jQuery权限管理系统第十三节--RBAC模式及ABP权限管理(附赠福利)

    ABP+AdminLTE+Bootstrap Table权限管理系统一期 Github:https://github.com/Jimmey-Jiang/ABP-ASP.NET-Boilerplate- ...

  8. ABP module-zero +AdminLTE+Bootstrap Table+jQuery权限管理系统第十四节--后台工作者HangFire与ABP框架Abp.Hangfire及扩展...

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 HangFire与Quartz.NET相比主要是HangFire的内置提供集成化的控制台,方便后台查看及监控,对于 ...

  9. ABP module-zero +AdminLTE+Bootstrap Table+jQuery权限管理系统第十七节--Quartz与ABP框架Abp.Quar...

    ABP+AdminLTE+Bootstrap Table权限管理系统一期 Github:https://github.com/Jimmey-Jiang/ABP-ASP.NET-Boilerplate- ...

最新文章

  1. BAD SLAM:捆绑束调整直接RGB-D SLAM
  2. “接口”的定义及其与“抽象类”的区别
  3. 9.Spring Security添加记住我功能
  4. linux内置变量大全,Linux进阶之bash编程四(内置变量)
  5. java直接调用复制文件,java中文件复制的4种方式,java文件的复制
  6. 如果已经知道某一CALL的具体作用,能否把后面所有相同的CALL都改成函数名形式?
  7. Android组件化专题-路由动态注入跳转参数以及获取其他模块的fragment
  8. MFC Attach()函数和Detach()函数
  9. python自然场景文字识别_chineseocr
  10. python ssh shell交互_使用Paramiko在Python上用ssh实现交互式shell?
  11. Python3+RobotFramework+pycharm
  12. OpenCV探索之路(九):模板匹配
  13. JAVA学习第八天--Scanner /String/StringBuilder
  14. Linux文件系统(五)---三大缓冲区之buffer块缓冲区
  15. php不使用框架,导出Excel,这里有代码,全解
  16. Eclipse的各种查找,类的查找,方法查找快捷键
  17. IDEA 前台静态文件发布不即时生效问题解决
  18. php doctrine datetime,关于php:Doctrine 2.1 – datetime列的默认值
  19. 塔米狗2022年地方国企名单,总计816家企业
  20. 【优雅的避坑】不安全!别再共享SimpleDateFormat变量了

热门文章

  1. BZOJ1054(搜索)
  2. activeMQ在文件上传的应用
  3. node中的Stream-Readable和Writeable解读
  4. HTML5 上传图片预览
  5. VisualStudio中的代码段
  6. attr与prop的区别
  7. J2ME最佳实践之联网开发-编写反应灵敏的联网提示界面
  8. c/c++中const用法总结
  9. docker linux k8s kubeadm
  10. Spring boot 中pom.xml 各个节点详解