Example #1
0
        public IActionResult CheckToken([FromBody] CheckTokenRequest request)
        {
            var tokenHandler         = new JwtSecurityTokenHandler();
            var validationParameters = GetValidationParameters();

            SecurityToken validatedToken;
            IPrincipal    principal = tokenHandler.ValidateToken(request.Token, validationParameters, out validatedToken);
            var           result    = principal.Identity == null ? false : true;

            //var token = new JwtSecurityTokenHandler().WriteToken(jwtToken.GenerateToken(userRepository, found));
            return(Ok(new
            {
                isValid = result
            }));
        }
Example #2
0
        public static TokenData tokenData(BodyRequest request, BodyResponse response, int role)
        {
            TokenData tokenData = null;

            if (!Configs.DEBUG_MODE)
            {
                //var thread = new Thread((object t) =>
                //{
                int  times  = 3;
                bool fail   = true;
                var  client = new HttpClient();

                CheckTokenRequest ctRequest = new CheckTokenRequest();
                ctRequest.Token         = request.Token;
                ctRequest.TokenPassword = Configs.TOKEN_PASSWORD;
                ctRequest.Role          = role;

                do
                {
                    try
                    {
                        var result = client.PostAsync(Configs.CHECK_TOKEN, new StringContent(JsonConvert.SerializeObject(ctRequest), Encoding.UTF8, "application/json")).Result.Content.ReadAsAsync <CheckTokenResponse>().Result;
                        fail = result == null;
                        if (!fail)
                        {
                            response.IsTokenTimeout = result.IsTokenTimeout;
                            if (result.IsError)
                            {
                                response.Errors.Add("Không thể truy cập đến máy chủ.");
                                response.IsError = true;
                            }
                            else
                            {
                                tokenData = result.Data;
                            }
                            break;
                        }
                    }
                    catch { }
                } while (fail && --times > 0);
                //(t as Thread).Abort();
                //});
                //thread.Start(thread);
            }
            return(tokenData);
        }
 /// <summary>
 /// Checks if the Token is valid
 /// this will contact the Manastone server and check if the token is valid
 /// a token check has to be Online only because the token is to authenticate a program client to a program server or service
 /// if the server doesn't answer it will throw a ManastoneOfferNotReceivedCorrectlyException
 /// </summary>
 /// <param name="req">CheckTokenRequest</param>
 /// <returns></returns>
 public bool CheckToken(CheckTokenRequest req)
 {
     try
     {
         _database.Log.AddToLogList(FnLog.FnLog.LogType.DrmLog, "CheckToken", "Start");
         var offer = SocketIoClient.RetrieveSingleValue <CheckTokenOffer>(_url, "CheckTokenOffer",
                                                                          "CheckTokenRequest", req.Serialize(), timeout: _timeout);
         _database.Log.AddToLogList(FnLog.FnLog.LogType.DrmLog, "CheckToken", "Complete");
         _database.Log.ProcessLogList();
         return(offer.IsValid);
     }
     catch (Exception e)
     {
         _database.Log.AddToLogList(FnLog.FnLog.LogType.DrmError, "ERROR on CheckToken", e.Message);
         _database.Log.ProcessLogList();
         throw new ManastoneOfferNotReceivedCorrectlyException();
     }
 }
        public HttpResponseMessage CheckToken([FromBody] CheckTokenRequest request)
        {
            var response = new CheckTokenResponse();

            try
            {
                if (request.TokenPassword == Configs.TOKEN_PASSWORD)
                {
                    var token = Token.Get(request.Token);
                    if (token == null)
                    {
                        response.IsTokenTimeout = true;
                    }
                    else
                    {
                        if (token.CapPQ == request.Role)
                        {
                            response.Data = new TokenData
                            {
                                UserId          = token.MaNV,
                                PermissionLevel = token.CapPQ,
                                AgencyId        = token.MaCN,
                                PermissionId    = token.MaPQ,
                            };
                        }
                        else
                        {
                            response.IsError = true;
                        }
                    }
                }
                else
                {
                    response.IsError = true;
                }
            }
            catch
            {
                response.IsError = true;
            }
            return(Request.CreateResponse(HttpStatusCode.OK, response));
        }