AccountController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Security.Claims;
  3. using System.Threading.Tasks;
  4. using System.Web.Http;
  5. using Abp.UI;
  6. using Abp.Web.Models;
  7. using Abp.WebApi.Controllers;
  8. using IwbZero;
  9. using IwbZero.Authorization.Base;
  10. using IwbZero.ToolCommon.StringModel;
  11. using Microsoft.Extensions.Internal;
  12. using Microsoft.Owin.Security;
  13. using Microsoft.Owin.Security.OAuth;
  14. using WeEngine.Api.Models;
  15. namespace WeEngine.Api.Controllers
  16. {
  17. public class AccountController : AbpApiController
  18. {
  19. public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; }
  20. //private readonly LogInManager _logInManager;
  21. static AccountController()
  22. {
  23. OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
  24. }
  25. public AccountController()
  26. {
  27. //_logInManager = logInManager;
  28. LocalizationSourceName = IwbZeroConsts.LocalizationSourceName;
  29. }
  30. [HttpPost]
  31. public async Task<AjaxResponse> Authenticate(LoginModel loginModel)
  32. {
  33. CheckModelState();
  34. //var loginResult = await GetLoginResultAsync(
  35. // loginModel.UsernameOrEmailAddress,
  36. // loginModel.Password,
  37. // loginModel.TenancyName
  38. // );
  39. var loginResult = await Login(loginModel);
  40. var ticket = new AuthenticationTicket(loginResult, new AuthenticationProperties());
  41. var currentUtc = new SystemClock().UtcNow;
  42. ticket.Properties.IssuedUtc = currentUtc;
  43. ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));
  44. return new AjaxResponse(OAuthBearerOptions.AccessTokenFormat.Protect(ticket));
  45. }
  46. private Task<ClaimsIdentity> Login(LoginModel loginModel)
  47. {
  48. if (loginModel.UsernameOrEmailAddress.IsEmpty()|| loginModel.Password.IsEmpty())
  49. {
  50. throw new UserFriendlyException("用户名或密码不能为空。");
  51. }
  52. //检查用户登录
  53. var flag = "Y".ValB();
  54. if (!flag)
  55. {
  56. throw new UserFriendlyException("密码验证失败。");
  57. }
  58. var claim = new ClaimsIdentity();
  59. claim.AddClaim(new Claim(ClaimTypes.Name, loginModel.UsernameOrEmailAddress));
  60. return Task.FromResult(claim);
  61. }
  62. //private async Task<IwbLoginResult<Tenant, User>> GetLoginResultAsync(string usernameOrEmailAddress, string password, string tenancyName)
  63. //{
  64. // var loginResult = await _logInManager.LoginAsync(usernameOrEmailAddress, password, tenancyName);
  65. // switch (loginResult.Result)
  66. // {
  67. // case IwbLoginResultType.Success:
  68. // return loginResult;
  69. // default:
  70. // throw CreateExceptionForFailedLoginAttempt(loginResult.Result, usernameOrEmailAddress, tenancyName);
  71. // }
  72. //}
  73. private Exception CreateExceptionForFailedLoginAttempt(IwbLoginResultType result, string usernameOrEmailAddress, string tenancyName)
  74. {
  75. switch (result)
  76. {
  77. case IwbLoginResultType.Success:
  78. return new ApplicationException("Don't call this method with a success result!");
  79. case IwbLoginResultType.InvalidUserNameOrEmailAddress:
  80. case IwbLoginResultType.InvalidPassword:
  81. return new UserFriendlyException(L("LoginFailed"), L("InvalidUserNameOrPassword"));
  82. case IwbLoginResultType.InvalidTenancyName:
  83. return new UserFriendlyException(L("LoginFailed"), L("ThereIsNoTenantDefinedWithName{0}", tenancyName));
  84. case IwbLoginResultType.TenantIsNotActive:
  85. return new UserFriendlyException(L("LoginFailed"), L("TenantIsNotActive", tenancyName));
  86. case IwbLoginResultType.UserIsNotActive:
  87. return new UserFriendlyException(L("LoginFailed"), L("UserIsNotActiveAndCanNotLogin", usernameOrEmailAddress));
  88. case IwbLoginResultType.UserEmailIsNotConfirmed:
  89. return new UserFriendlyException(L("LoginFailed"), "Your email address is not confirmed. You can not login"); //TODO: localize message
  90. default: //Can not fall to default actually. But other result types can be added in the future and we may forget to handle it
  91. Logger.Warn("Unhandled login fail reason: " + result);
  92. return new UserFriendlyException(L("LoginFailed"));
  93. }
  94. }
  95. protected virtual void CheckModelState()
  96. {
  97. if (!ModelState.IsValid)
  98. {
  99. throw new UserFriendlyException("Invalid request!");
  100. }
  101. }
  102. }
  103. }