Example #1
0
        public IHttpActionResult Authenticate([FromBody] User user)
        {
            if (user.Name == testUser.Name && user.Password == testUser.Password)
            {
                DateTime actualDate = DateTime.Now.ToLocalTime();
                var      obj        = new LogedIn("user", actualDate, user.Name, 1);

                string token = Jose.JWT.Encode(obj, secretKey, JwsAlgorithm.HS256);

                HttpResponseMessage OkMessage = new HttpResponseMessage(HttpStatusCode.OK);
                //Body
                OkMessage = Request.CreateResponse(HttpStatusCode.OK, token);

                //Header
                OkMessage.Headers.Add("Authorized", "Access permited!");
                OkMessage.Headers.Add("Algorithm", "HS256");

                return(ResponseMessage(OkMessage));
            }
            else
            {
                string err = "INCORECT";
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                response = Request.CreateResponse(HttpStatusCode.OK, err);
                response.Headers.Add("Unauthorized", "No Access!");
                return(ResponseMessage(response));
            }
        }
Example #2
0
        public IHttpActionResult Authorize()
        {
            //toate headerele
            var x = Request.Headers;

            //headerul cu tokenul
            string token = x.GetValues("token").First();

            //decodarea si afisarea in clar a informatiilor
            LogedIn tokenDecodat = new LogedIn();

            tokenDecodat = JWT.Decode <LogedIn>(token, secretKey, JwsAlgorithm.HS256);

            HttpResponseMessage OkMessage = new HttpResponseMessage(HttpStatusCode.OK);

            //Body
            OkMessage = Request.CreateResponse(HttpStatusCode.OK, tokenDecodat);

            return(ResponseMessage(OkMessage));
        }