/// <summary> /// 创建Windows凭据 /// </summary> /// <param name="key">IP或者网络地址</param> /// <param name="userName">用户名</param> /// <param name="password">用户密码</param> /// <returns></returns> public static bool CreateCred(string key, string userName, string password) { //用于标记凭据添加是否成功 i=0:添加成功;i=1:添加失败 int i = 0; try { i = NativeCredMan.WriteCred(key, userName, password, CRED_TYPE.DOMAIN_PASSWORD, CRED_PERSIST.LOCAL_MACHINE); } catch { i = 1; } if (i == 0) { return(true); } else { return(false); } }
/// <summary> /// 查询凭据是否存在 /// </summary> /// <param name="targetName">IP或者网络地址</param> /// <returns>是否存在</returns> public static bool QueryCred(string targetName) { IntPtr intPtr = new IntPtr(); bool flag = false; try { flag = NativeCredMan.WReadCred(targetName, CRED_TYPE.DOMAIN_PASSWORD, 1, out intPtr); } catch { flag = false; } return(flag); }
/// <summary> /// 删除凭据 /// </summary> /// <param name="targetName">IP或者网络地址</param> /// <returns>是否删除成功</returns> public static bool DeleteCred(string targetName) { bool flag = false; try { IntPtr intPtr = new IntPtr(); if (NativeCredMan.WReadCred(targetName, CRED_TYPE.DOMAIN_PASSWORD, 1, out intPtr)) { flag = NativeCredMan.DeleteCred(targetName.Trim(), CRED_TYPE.DOMAIN_PASSWORD, 0); } else { flag = true; } } catch { flag = false; } return(flag); }