public CreateSessionQueryHandler(CreateSessionQuery query, IUnityContainer unityContainer)
     : base(unityContainer)
 {
     _query = query;
 }
Example #2
0
        public HttpResponseMessage Login([FromBody] JObject data)
        {
            JToken parameterUsername = data["Username"];
            JToken parameterPassword = data["Password"];

            try
            {
                if (string.IsNullOrEmpty(parameterUsername.ToString()))
                {
                    throw new ArgumentNullException(nameof(parameterUsername));
                }

                if (string.IsNullOrEmpty(parameterPassword.ToString()))
                {
                    throw new ArgumentNullException(nameof(parameterPassword));
                }

                LoginQuery loginQuery = new LoginQuery()
                {
                    Username = parameterUsername.ToString(),
                    Password = parameterPassword.ToString()
                };

                LoginQueryHandler loginQueryHandler = new LoginQueryHandler(loginQuery, UnityContainer);

                Guid personUid = loginQueryHandler.Handle();

                HttpCookie requestCookie = HttpContext.Current.Request.Cookies.Get(ConfigurationManager.AppSettings["SessionCookieName"]);

                if (requestCookie == null)
                {
                    CreateSessionQuery createSessionQuery = new CreateSessionQuery()
                    {
                        Session = new SessionDomainModel()
                        {
                            LoginUID   = personUid,
                            ExpireDate = DateTimeOffset.Now.AddDays(1)
                        }
                    };

                    CreateSessionQueryHandler createSessionQueryHandler = new CreateSessionQueryHandler(createSessionQuery, new UnityContainer());

                    var guid = createSessionQueryHandler.Handle().ToString();

                    HttpCookie cookie = new HttpCookie(ConfigurationManager.AppSettings["SessionCookieName"])
                    {
                        Value    = guid,
                        Domain   = HttpContext.Current.Request.Url.Host,
                        Expires  = DateTime.Now.AddDays(1),
                        HttpOnly = false,
                    };

                    var ersponse = HttpContext.Current.Response;

                    HttpContext.Current.Response.AppendCookie(cookie);

                    return(Request.CreateResponse(HttpStatusCode.OK, guid));
                }

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }