KeePassLib.Keys.CompositeKey.ContainsType C# (CSharp) Method

ContainsType() public method

Test whether the composite key contains a specific type of user keys (password, key file, ...). If at least one user key of that type is present, the function returns true.
public ContainsType ( Type tUserKeyType ) : bool
tUserKeyType System.Type User key type.
return bool
        public bool ContainsType(Type tUserKeyType)
        {
            Debug.Assert(tUserKeyType != null);
            if(tUserKeyType == null) throw new ArgumentNullException("tUserKeyType");

            foreach(IUserKey pKey in m_vUserKeys)
            {
                if(pKey == null) { Debug.Assert(false); continue; }

            #if KeePassUAP
                if(pKey.GetType() == tUserKeyType)
                    return true;
            #else
                if(tUserKeyType.IsInstanceOfType(pKey))
                    return true;
            #endif
            }

            return false;
        }

Usage Example

Ejemplo n.º 1
0
		private static KdbErrorCode SetDatabaseKey(KdbManager mgr, CompositeKey pwKey)
		{
			KdbErrorCode e;

			bool bPassword = pwKey.ContainsType(typeof(KcpPassword));
			bool bKeyFile = pwKey.ContainsType(typeof(KcpKeyFile));

			string strPassword = (bPassword ? (pwKey.GetUserKey(
				typeof(KcpPassword)) as KcpPassword).Password.ReadString() : string.Empty);
			string strKeyFile = (bKeyFile ? (pwKey.GetUserKey(
				typeof(KcpKeyFile)) as KcpKeyFile).Path : string.Empty);

			if(bPassword && bKeyFile)
				e = mgr.SetMasterKey(strKeyFile, true, strPassword, IntPtr.Zero, false);
			else if(bPassword && !bKeyFile)
				e = mgr.SetMasterKey(strPassword, false, null, IntPtr.Zero, false);
			else if(!bPassword && bKeyFile)
				e = mgr.SetMasterKey(strKeyFile, true, null, IntPtr.Zero, false);
			else if(pwKey.ContainsType(typeof(KcpUserAccount)))
				throw new Exception(KPRes.KdbWUA);
			else throw new Exception(KLRes.InvalidCompositeKey);

			return e;
		}