using System; using System.Collections.Generic; using System.Text; using System.Web.Services; using System.Configuration; using ZetaLib.Core.Common; using ZetaLib.Core.Collections; /// <summary> /// Base class for web services that protect their functions with /// an API key. /// </summary> public class ApiKeyProtectedWebServiceBase : WebService { /// <summary> /// Checks whether the API key is valid. /// </summary> protected void CheckApiKey( string apiKey ) { if ( validApiKeys.Length > 0 ) { foreach ( string validApiKey in validApiKeys ) { if ( string.Compare( apiKey, validApiKey, true ) == 0 ) { return; } } // Invalue. throw new ApplicationException( string.Format( "Invalid API key '{0}'.", apiKey ) ); } } private static string apiKeyConfigName = @"apiKey"; /// <summary> /// Allows for changing the app settings key. /// </summary> protected static string ApiKeyConfigName { get { lock ( typeLock ) { return apiKeyConfigName; } } set { lock ( typeLock ) { if ( apiKeyConfigName != value ) { apiKeyConfigName = value; _validApiKeys = null; } } } } private static string[] _validApiKeys; private static object typeLock = new object(); /// <summary> /// /// </summary> private static string[] validApiKeys { get { if ( _validApiKeys == null ) { lock ( typeLock ) { Set<string> result = new Set<string>(); result.AddRange( ConvertHelper.ToString( ConfigurationManager.AppSettings[ apiKeyConfigName], string.Empty ).Split( new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries ) ); _validApiKeys = result.ToArray(); } } return _validApiKeys; } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)