Exemple #1
0
        private CachedUserToken GetSQLCachedUserToken(string clientId, string userId, string resource)
        {
            CachedUserToken sqlCachedUser;

            using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["TokenCacheSqlConnectionString"]))
            {
                SqlCommand cmd = new SqlCommand("extended.GetUserToken", con);
                cmd.Parameters.AddWithValue("@ClientId", clientId);
                cmd.Parameters.AddWithValue("@UserId", userId);
                cmd.Parameters.AddWithValue("@Resource", resource);
                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    reader.Read();

                    sqlCachedUser = null;

                    if (reader.HasRows)
                    {
                        sqlCachedUser = new CachedUserToken();
                        sqlCachedUser.userTokenCacheId = reader.GetInt32(0);
                        sqlCachedUser.cacheBits        = (byte[])reader.GetValue(1);
                        sqlCachedUser.lastWrite        = reader.GetDateTime(2);
                    }
                }
            }

            return(sqlCachedUser);
        }
Exemple #2
0
        void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            if (this.HasStateChanged)
            {
                if (cache == null)
                {
                    cache = new CachedUserToken();
                }

                cache.cacheBits = MachineKey.Protect(this.Serialize(), "ADALCache");
                cache.lastWrite = DateTime.Now;

                if (cache.userTokenCacheId == 0)
                {
                    int cacheId = AddSQLCachedUserToken(args.ClientId, args.DisplayableId, args.Resource, cache);
                    cache.userTokenCacheId = cacheId;
                }
                else
                {
                    UpdateSQLCachedUserToken(cache);
                }

                this.HasStateChanged = false;
            }
        }
Exemple #3
0
        private void UpdateSQLCachedUserToken(CachedUserToken token)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["TokenCacheSqlConnectionString"]))
            {
                SqlCommand cmd = new SqlCommand("extended.UpdateUserToken", con);
                cmd.Parameters.AddWithValue("@UserTokenCacheId", token.userTokenCacheId);
                cmd.Parameters.AddWithValue("@CachedBits", token.cacheBits);
                cmd.Parameters.AddWithValue("@LastWrite", token.lastWrite);

                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
Exemple #4
0
        void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {
            if (cache == null)
            {
                cache = GetSQLCachedUserToken(args.ClientId, args.DisplayableId, args.Resource);
            }
            else
            {
                DateTime lastWrite = GetSQLCachedUserLastWrite(args.ClientId, args.DisplayableId, args.Resource);

                if (lastWrite > cache.lastWrite)
                {
                    cache = GetSQLCachedUserToken(args.ClientId, args.UniqueId, args.Resource);
                }
            }
            this.Deserialize((cache == null) ? null : MachineKey.Unprotect(cache.cacheBits, "ADALCache"));
        }
Exemple #5
0
        private int AddSQLCachedUserToken(string clientId, string userId, string resource, CachedUserToken token)
        {
            int cacheId;

            using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["TokenCacheSqlConnectionString"]))
            {
                SqlCommand cmd = new SqlCommand("extended.AddUserToken", con);
                cmd.Parameters.AddWithValue("@ClientId", clientId);
                cmd.Parameters.AddWithValue("@UserId", userId);
                cmd.Parameters.AddWithValue("@Resource", resource);
                cmd.Parameters.AddWithValue("@CachedBits", token.cacheBits);
                cmd.Parameters.AddWithValue("@LastWrite", token.lastWrite);

                SqlParameter UserTokenCacheIdParam = new SqlParameter("@UserTokenCacheId", SqlDbType.Int);
                UserTokenCacheIdParam.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(UserTokenCacheIdParam);

                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();
                cmd.ExecuteNonQuery();

                cacheId = (int)cmd.Parameters["@UserTokenCacheId"].Value;
            }

            return(cacheId);
        }