/// <summary> /// Function to store an access token. The user id is part of the userAccessToken object /// </summary> public static bool storeUserAccessToken(userAccessToken accessToken) { bool rval = true; string sql = string.Empty; userAccessToken cacheToken = getUserAccessToken(accessToken.userId); using (SqlConnection dbcon = new SqlConnection(_camsConnectionString)) { dbcon.Open(); using (SqlCommand cmd = new SqlCommand("dbo.storeUserAccessToken", dbcon)) { cmd.CommandType = CommandType.Text; if (cacheToken == null) { sql = string.Format("insert into accesstokencache (userid, accesstoken, refreshtoken, tokenlife, tstamp, responseUrl) values ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}')", accessToken.userId, accessToken.accessToken, accessToken.refreshToken, accessToken.tokenLife, accessToken.tstamp.ToString(), accessToken.responseUrl); } else { sql = string.Format("update accesstokencache set accesstoken='{0}', tokenlife='{1}', tstamp='{2}', responseUrl='{4}' where userid = '{3}'", accessToken.accessToken, accessToken.tokenLife, accessToken.tstamp.ToString(), accessToken.userId, accessToken.responseUrl); } cmd.CommandText = sql; cmd.ExecuteNonQuery(); } } return(rval); }
/// <summary> /// Function to retrieve a stored access token, provided a user id /// </summary> public static userAccessToken getUserAccessToken(long userId) { userAccessToken rval = null; string sql = "select * from accessTokenCache where userid = '" + userId.ToString() + "'"; using (SqlConnection dbcon = new SqlConnection(_camsConnectionString)) { dbcon.Open(); using (SqlCommand cmd = new SqlCommand("dbo.getUserAccessToken", dbcon)) { cmd.CommandType = CommandType.Text; cmd.CommandText = sql; DataSet ds = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(ds); if (ds != null && ds.Tables[0].Rows.Count == 1) { //we found an existing token, return the token helper object rval = new userAccessToken(ds.Tables[0].Rows[0]); } } } return(rval); }