/Diamond_Prod/ClientWeb/Services/SecurityService/UsersController.cs

# · C# · 124 lines · 104 code · 17 blank · 3 comment · 20 complexity · 5bb72cfbea03921b69a8a15cc15e9f57 MD5 · raw file

  1. namespace Home.Services.SecurityService
  2. {
  3. using System;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Security.Claims;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web.Http;
  12. using System.Web.OData;
  13. using System.Web.OData.Query;
  14. using System.Web.OData.Routing;
  15. public class UsersController : BaseODataController
  16. {
  17. private readonly SecurityDbContext database = new SecurityDbContext();
  18. // GET odata/Users
  19. [HttpGet]
  20. [EnableQuery(PageSize = 50)]
  21. public IHttpActionResult Get()
  22. {
  23. var users = this.database.Users.Where(r => !r._IsLocked);
  24. return Ok(users);
  25. }
  26. // GET odata/Users('id')
  27. [HttpGet]
  28. [EnableQuery]
  29. [ODataRoute("Users({id})")]
  30. public Task<IHttpActionResult> Get([FromODataUri] Guid id, ODataQueryOptions<User> options)
  31. {
  32. var users = this.database.Users.Where(r => r.Id == id && !r._IsLocked);
  33. return GetODataSingleAsync(users, options);
  34. }
  35. // GET odata/Users('id')/Property
  36. [HttpGet]
  37. [ODataRoute("Users({id})/LoginName")]
  38. [ODataRoute("Users({id})/DisplayName")]
  39. [ODataRoute("Users({id})/Role")]
  40. [ODataRoute("Users({id})/Created")]
  41. [ODataRoute("Users({id})/CreatedBy")]
  42. [ODataRoute("Users({id})/Modified")]
  43. [ODataRoute("Users({id})/ModifiedBy")]
  44. public async Task<IHttpActionResult> GetProperty([FromODataUri] Guid id)
  45. {
  46. var user = await this.database.Users.FirstOrDefaultAsync(r => r.Id == id && !r._IsLocked);
  47. return GetODataProperty(user);
  48. }
  49. [HttpPost]
  50. [ODataRoute("Login(name={name}, password={password})")]
  51. public async Task<IHttpActionResult> Login(string name, string password)
  52. {
  53. if (name == null || password == null)
  54. {
  55. return PreconditionRequired();
  56. }
  57. if (name.Length == 0 || password.Length == 0)
  58. {
  59. return StatusCode(HttpStatusCode.PreconditionFailed);
  60. }
  61. var values = await this.database.Users.Where(u => u.LoginName == name && !u._IsLocked)
  62. .Select(u => new { u.DisplayName, u.Role, Password = u._Password })
  63. .FirstOrDefaultAsync();
  64. if (values == null)
  65. {
  66. return StatusCode(HttpStatusCode.PreconditionFailed);
  67. }
  68. if (password != values.Password)
  69. {
  70. return StatusCode(HttpStatusCode.PreconditionFailed);
  71. }
  72. var identity = new ClaimsIdentity("Forms");
  73. identity.AddClaim(new Claim(ClaimTypes.Name, values.DisplayName));
  74. identity.AddClaim(new Claim(ClaimTypes.Role, values.Role.ToString()));
  75. Request.GetOwinContext().Authentication.SignIn(identity);
  76. return Ok(new LoginResult() { Name = values.DisplayName, Role = values.Role.ToString() });
  77. }
  78. [HttpPost]
  79. [ODataRoute("Login()")]
  80. public IHttpActionResult Logout()
  81. {
  82. Request.GetOwinContext().Authentication.SignOut("Forms");
  83. return Ok();
  84. }
  85. [HttpGet]
  86. [ODataRoute("CheckLogin()")]
  87. public IHttpActionResult CheckLogin()
  88. {
  89. var result = new LoginResult();
  90. var user = Request.GetOwinContext().Authentication.User;
  91. if (user.Identity.IsAuthenticated)
  92. {
  93. result.Name = user.Identity.Name;
  94. result.Role = user.FindFirst(ClaimTypes.Role).Value;
  95. }
  96. return Ok(result);
  97. }
  98. protected override void Dispose(bool disposing)
  99. {
  100. if (disposing)
  101. {
  102. this.database.Dispose();
  103. }
  104. base.Dispose(disposing);
  105. }
  106. }
  107. }