Ejemplo n.º 1
0
 public IHttpActionResult GenerateToken([FromBody] ClientKey clientkeys)
 {
     try
     {
         if (string.IsNullOrEmpty(clientkeys.ClientId) && string.IsNullOrEmpty(clientkeys.ClientSecret))
         {
             return(BadRequest("Not Valid Request"));
         }
         else
         {
             if (_IAuthenticate.ValidateKeys(clientkeys))
             {
                 var keys = _IAuthenticate.GetClientKeysDetailsbyCLientIDandClientSecert(clientkeys.ClientId,
                                                                                         clientkeys.ClientSecret);
                 if (keys == null)
                 {
                     return(NotFound());
                 }
                 else
                 {
                     var result = _IAuthenticate.IsTokenAlreadyExists(clientkeys.DeviceId);
                     if (result)
                     {
                         _IAuthenticate.DeleteGenerateToken(clientkeys.DeviceId);
                         var token = GenerateandSaveToken(clientkeys);
                         return(Ok <TokenManager>(token));
                     }
                     else
                     {
                         var token = GenerateandSaveToken(clientkeys);
                         return(Ok <TokenManager>(token));
                     }
                 }
             }
             else
             {
                 return(NotFound());
             }
         }
     }
     catch (Exception e)
     {
         //Console.WriteLine(e);
         //throw;
         return(BadRequest());
     }
 }
        public HttpResponseMessage Authenticate(string ClientId, string ClientSecret)
        {
            ClientKeys ClientKeys = new ClientKeys()
            {
                ClientId = ClientId, ClientSecret = ClientSecret
            };

            if (string.IsNullOrEmpty(ClientKeys.ClientId) && string.IsNullOrEmpty(ClientKeys.ClientSecret))
            {
                HttpResponseMessage response = new HttpResponseMessage();
                response = Request.CreateResponse(HttpStatusCode.NotAcceptable, "Not Valid Request");
                return(response);
            }
            else if (_IAuthenticate.ValidateKeys(ClientKeys))
            {
                var clientkeys = _IAuthenticate.GetClientKeysDetailsbyCLientIDandClientSecret(ClientKeys.ClientId, ClientKeys.ClientSecret);

                if (clientkeys == null)
                {
                    HttpResponseMessage response = new HttpResponseMessage();
                    response = Request.CreateResponse(HttpStatusCode.NotFound, "InValid Keys");
                    return(response);
                }
                else
                {
                    if (_IAuthenticate.IsTokenAlreadyExists(clientkeys.RegisterAppId.Value))
                    {
                        _IAuthenticate.DeleteGenerateToken(clientkeys.RegisterAppId.Value);

                        return(GenerateandSaveToken(clientkeys));
                    }
                    else
                    {
                        return(GenerateandSaveToken(clientkeys));
                    }
                }
            }
            else
            {
                HttpResponseMessage response = new HttpResponseMessage();
                response = Request.CreateResponse(HttpStatusCode.NotFound, "InValid Keys");
                return(response);
            }
        }
        // POST: api/Authenticate
        public HttpResponseMessage Authenticate([FromBody] ClientKey ClientKeys)
        {
            if (string.IsNullOrEmpty(ClientKeys.ClientID) && string.IsNullOrEmpty(ClientKeys.ClientSecret))
            {
                var message = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
                message.Content = new StringContent("Not Valid Request");
                return(message);
            }
            else
            {
                if (_IAuthenticate.ValidateKeys(ClientKeys))
                {
                    var clientkeys = _IAuthenticate.GetClientKeysDetailsbyCLientIDandClientSecert(ClientKeys.ClientID, ClientKeys.ClientSecret);

                    if (clientkeys == null)
                    {
                        var message = new HttpResponseMessage(HttpStatusCode.NotFound);
                        message.Content = new StringContent("InValid Keys");
                        return(message);
                    }
                    else
                    {
                        if (_IAuthenticate.IsTokenAlreadyExists(clientkeys.CompanyID))
                        {
                            _IAuthenticate.DeleteGenerateToken(clientkeys.CompanyID);

                            return(GenerateandSaveToken(clientkeys));
                        }
                        else
                        {
                            return(GenerateandSaveToken(clientkeys));
                        }
                    }
                }
                else
                {
                    var message = new HttpResponseMessage(HttpStatusCode.NotFound);
                    message.Content = new StringContent("InValid Keys");
                    return(new HttpResponseMessage {
                        StatusCode = HttpStatusCode.NotAcceptable
                    });
                }
            }
        }
Ejemplo n.º 4
0
        public string GetToken(ClientKeys clientKeys, IAuthenticate authenticater, String tokenExpiry, out String sessiontoken)
        {
            //
            sessiontoken = null;

            //
            if (string.IsNullOrEmpty(clientKeys.ClientId) && string.IsNullOrEmpty(clientKeys.ClientSecret))
            {
                return("Not Valid Request");
            }

            //
            string result = string.Empty;

            if (authenticater.ValidateKeys(clientKeys))
            {
                var clientkeys = authenticater.GetClientKeysDetailsbyCLientIDandClientSecret(clientKeys.ClientId, clientKeys.ClientSecret);
                if (clientkeys == null)
                {
                    return("InValid Keys");
                }
                else
                {
                    if (authenticater.IsTokenAlreadyExists(clientkeys.RegisterAppId.Value))
                    {
                        authenticater.DeleteGenerateToken(clientkeys.RegisterAppId.Value);

                        var IssuedOn = DateTime.Now;
                        var newToken = authenticater.GenerateToken(clientkeys, IssuedOn);

                        var status = authenticater.InsertToken(clientkeys, tokenExpiry, newToken);
                        if (status == 1)
                        {
                            // Session["_T0k@n_"] = newToken;
                            sessiontoken = newToken;
                            result       = "Token generated successfully !!!";
                        }
                        else
                        {
                            result = "Error in Creating Token";
                        }
                    }
                    else
                    {
                        var issuedOn = DateTime.Now;
                        var newToken = authenticater.GenerateToken(clientkeys, issuedOn);

                        var status = authenticater.InsertToken(clientkeys, tokenExpiry, newToken);
                        if (status == 1)
                        {
                            // Session["_T0k@n_"] = newToken;
                            sessiontoken = newToken;
                            result       = "Token generated successfully !!!";
                        }
                        else
                        {
                            result = "Error in Creating Token";
                        }
                    }
                }
            }
            else
            {
                result = "Invalid Keys!!!";
            }

            return(result);
        }