Advertisement
Guest User

Untitled

a guest
May 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. public class SecurityController : ApiController
  2. {
  3. static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
  4.  
  5. [HttpPost]
  6. public IHttpActionResult StartSession([FromBody] ConnectRequestModel requestModel)
  7. {
  8. var client = new SecurityFacade(); // from proxy class
  9. var requestMessage = new ConnectRequest
  10. {
  11. Username = requestModel.Username,
  12. Password = requestModel.Password
  13. };
  14.  
  15. try
  16. {
  17. return Ok(new
  18. {
  19. client.StartSession(requestMessage).SessionId
  20. });
  21. }
  22. catch (SoapException ex)
  23. {
  24. return BadRequest(new SoapExceptionResponse(ex.Code.Name, ex.Message));
  25. }
  26. catch (Exception ex)
  27. {
  28. Logger.Error(ex);
  29. return InternalServerError(ex);
  30. }
  31. }
  32. }
  33.  
  34. public class SoapExceptionResponse
  35. {
  36. public SoapExceptionResponse(string code, string message)
  37. {
  38. Code = code;
  39. Message = message;
  40. }
  41.  
  42. public string Code { get; }
  43. public string Message { get; }
  44.  
  45. public static implicit operator string(SoapExceptionResponse s)
  46. {
  47. return JsonConvert.SerializeObject(s);
  48. }
  49. }
  50.  
  51. {
  52. "Message": "{"Code":"InvalidUsernameAndPasswordPair","Message":"User/Password invalid."}"
  53. }
  54.  
  55. var request = new RestRequest("security/startsession");
  56. request.AddParameter("Username", Username);
  57. request.AddParameter("Password", Password);
  58.  
  59. restClient.PostAsync<ConnectResponse>(request, (response, handle) =>
  60. {
  61. if (response.StatusCode == HttpStatusCode.OK)
  62. {
  63. var sessionId = response.Data.SessionId;
  64. }
  65. else if (response.StatusCode == HttpStatusCode.BadRequest)
  66. {
  67. var responseMessage = JsonConvert.DeserializeAnonymousType(response.Content, new { Message = "" });
  68. var error = JsonConvert.DeserializeAnonymousType(responseMessage.Message, new { Code = "", Message = "" });
  69. var code = error.Code;
  70. var message = error.Message;
  71.  
  72. if (code == "InvalidUsernameAndPasswordPair")
  73. {
  74. // todo
  75. }
  76. }
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement