Exemplo n.º 1
0
        private TokenManager GenerateandSaveToken(ClientKey clientkeys)
        {
            var          IssuedOn = DateTime.Now;
            var          newToken = _IAuthenticate.GenerateToken(clientkeys, IssuedOn);
            TokenManager token    = new TokenManager();

            token.TokenId   = 0;
            token.TokenKey  = newToken;
            token.DeviceId  = clientkeys.DeviceId;
            token.IssueOn   = IssuedOn;
            token.ExpiersOn = DateTime.Now.AddMinutes(Convert.ToInt32(ConfigurationManager.AppSettings["TokenExpiry"]));
            token.CreatedOn = DateTime.Now;
            var result = _IAuthenticate.InsertToken(token);

            return(token);
            //if (result == 1)
            //{
            //    HttpResponseMessage response = new HttpResponseMessage();
            //    response = Request.CreateResponse(HttpStatusCode.OK, "Authorized");
            //    response.Headers.Add("Token", newToken);
            //    response.Headers.Add("TokenExpiry", ConfigurationManager.AppSettings["TokenExpiry"]);
            //    response.Headers.Add("Access-Control-Expose-Headers", "Token,TokenExpiry");
            //    return response;
            //}
            //else
            //{
            //    var message = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
            //    message.Content = new StringContent("Error in Creating Token");
            //    return message;
            //}
        }
        private HttpResponseMessage GenerateandSaveToken(ClientKeys clientkeys)
        {
            var IssuedOn = DateTime.Now;
            var newToken = _IAuthenticate.GenerateToken(clientkeys, IssuedOn);
            var result   = _IAuthenticate.InsertToken(clientkeys, ConfigurationManager.AppSettings["TokenExpiry"], newToken);

            if (result == 1)
            {
                HttpResponseMessage response = new HttpResponseMessage();
                response = Request.CreateResponse(HttpStatusCode.OK, newToken);
                response.Headers.Add("Token", newToken);
                response.Headers.Add("TokenExpiry", ConfigurationManager.AppSettings["TokenExpiry"]);
                response.Headers.Add("Access-Control-Expose-Headers", "Token,TokenExpiry");
                return(response);
            }
            else
            {
                HttpResponseMessage response = new HttpResponseMessage();
                response = Request.CreateResponse(HttpStatusCode.NotAcceptable, "Error in Creating Token");
                return(response);
            }
        }
Exemplo n.º 3
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);
        }