コード例 #1
0
        public bool ValidateToken(TokenModel token)
        {
            var filter = Builders <BsonDocument> .Filter.Eq("AuthToken", token.AuthToken);

            var projection = Builders <BsonDocument> .Projection.Exclude("_id");

            var        docs          = tokenCollection.Find(filter).Project(projection).ToList();
            TokenModel existingToken = Newtonsoft.Json.JsonConvert.DeserializeObject <TokenModel>(docs[0].ToJson());

            DateTime existingTokenExpiresOn = Convert.ToDateTime(existingToken.ExpiresOn);

            if (existingToken != null && !(DateTime.Now > existingTokenExpiresOn))
            {
                existingToken.ExpiresOn = existingTokenExpiresOn.AddSeconds(
                    Convert.ToDouble(ConfigurationManager.AppSettings["AuthTokenExpiry"])).ToString("yyyy-MM-dd HH:mm:ss");
                try
                {
                    BsonDocument doc = BsonSerializer.Deserialize <BsonDocument>(existingToken.ToJson());
                    tokenCollection.ReplaceOneAsync(filter, doc);
                }
                catch (System.Exception ex)
                {
                    throw new Exception("Error occured while inserting token" + ex.Message);
                }
                return(true);
            }
            return(false);
        }
コード例 #2
0
        public TokenModel GenerateToken(string username)
        {
            //first delete existing token if available
            var filter = Builders <BsonDocument> .Filter.Eq("UserName", username);

            var projection = Builders <BsonDocument> .Projection.Exclude("_id");


            if (tokenCollection.Find(filter).Project(projection) != null)
            {
                var docs = tokenCollection.Find(filter).Project(projection).ToList();

                if (docs != null && docs.Count > 0)
                {
                    TokenModel existingToken = Newtonsoft.Json.JsonConvert.DeserializeObject <TokenModel>(docs[0].ToJson());

                    bool deleted = DeleteToken(existingToken);
                    if (!deleted)
                    {
                        return(null);
                    }
                }
            }

            //then create a new token
            string token      = Guid.NewGuid().ToString();
            string issuedOn   = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string expiredOn  = DateTime.Now.AddSeconds(Convert.ToDouble(ConfigurationManager.AppSettings["AuthTokenExpiry"])).ToString("yyyy-MM-dd HH:mm:ss");
            var    tokenModel = new TokenModel()
            {
                UserName  = username,
                IssuedOn  = issuedOn,
                ExpiresOn = expiredOn,
                AuthToken = token
            };

            try
            {
                var jsonWriterSettings = new JsonWriterSettings {
                    OutputMode = JsonOutputMode.Strict
                };
                BsonDocument doc = BsonSerializer.Deserialize <BsonDocument>(tokenModel.ToJson(jsonWriterSettings));

                tokenCollection.InsertOneAsync(doc);
            }
            catch (System.Exception ex)
            {
                throw new Exception("Error occured while inserting token" + ex.Message);
                //return null;
            }

            return(tokenModel);
        }
コード例 #3
0
        public void Test1()
        {
            var model = new TokenModel
            {
                AccessToken  = Rand.NextString(32),
                TokenType    = "token",
                ExpireIn     = 7200,
                RefreshToken = Rand.NextString(32),
            };

            var json = model.ToJson();

            var rs = "{\"access_token\":\"{access_token}\",\"token_type\":\"token\",\"expire_in\":7200,\"refresh_token\":\"{refresh_token}\"}";

            rs = rs.Replace("{access_token}", model.AccessToken)
                 .Replace("{refresh_token}", model.RefreshToken);

            Assert.Equal(rs, json);
        }