|
|
<
C# 考证账号密码的办法类
媒介
一样平常C#开辟过程当中伴侣们大要会碰到需求考证账号密码的操作,自己经由过程屡次考证(踩坑无数),终究找到了一个借算数比力靠谱的办法,如今总结分享给各人,年夜神们假如有更好的办法请正在再起区报告我哦! 话未几道 间接上代码
2.类
考证账号类:
- [DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
- //读与根据疑息
- static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag, out IntPtr CredentialPtr);
- [DllImport("Advapi32.dll", EntryPoint = "CredWriteW", CharSet = CharSet.Unicode, SetLastError = true)]
- //增长根据
- static extern bool CredWrite([In] ref NativeCredential userCredential, [In] UInt32 flags);
- [DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)]
- static extern bool CredFree([In] IntPtr cred);
- [DllImport("Advapi32.dll", EntryPoint = "CredDeleteW", CharSet = CharSet.Unicode)]
- //删除根据
- static extern bool CredDelete(string target, CRED_TYPE type, int flags);
- [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- public static extern bool CredEnumerate(string filter, uint flag, out uint count, out IntPtr pCredentials);
- //登岸考证
- [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- static extern bool LogonUser(string username, string domain,
- string password, LogonType logonType,
- LogonProvider logonProvider,
- out IntPtr userToken);
- [DllImport("Advapi32.DLL")]
- static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
复造代码 3.留意事项
Interactive =2 当地交互登录。最多见的登录方法。
Network=3收集登录 - 最多见的是会见收集同享文件夹或挨印机。IIS的认证也是Type 3
Batch=4 谋划使命
Service=5 效劳
某些效劳是用一个域帐号去运转的,呈现Failure常睹的状况是办理员变动了域帐号密码,可是遗忘重设Service中的帐号密码。
Unlock=7 解除屏幕锁定
很多公司皆有如许的宁静设置:当用户分开屏幕一段工夫后,屏保法式会锁定计较机屏幕。解开屏幕锁定需求键进用户名战密码。此时发生的日记范例便是Type 7
NetworkCleartext=8 收集明文登录 – 凡是发作正在IIS 的 ASP登录。没有保举
NewCredentials=9 新身份登录 – 凡是发作正在RunAS方法运转某法式时的登录考证。
RemoteInteractive=10 长途登录 – 好比Terminal service大概RDP方法。可是Windows 2000是出有Type10的,用Type 2。WindowsXP/2003起有Type 10
CachedInteractive=11 缓存登录:
- public static class WindowsCredTools
- {
- /// <summary>
- /// 根据范例
- /// </summary>
- public enum CRED_TYPE : uint
- {
- //一般根据
- GENERIC = 1,
- //域暗码
- DOMAIN_PASSWORD = 2,
- //域证书
- DOMAIN_CERTIFICATE = 3,
- //域可睹暗码
- DOMAIN_VISIBLE_PASSWORD = 4,
- //普通证书
- GENERIC_CERTIFICATE = 5,
- //域扩大
- DOMAIN_EXTENDED = 6,
- //最年夜
- MAXIMUM = 7,
- // Maximum supported cred type
- MAXIMUM_EX = (MAXIMUM + 1000), // Allow new applications to run on old OSes
- }
- //永世性
- public enum CRED_PERSIST : uint
- {
- SESSION = 1, //当地计较机
- LOCAL_MACHINE = 2, //企业
- ENTERPRISE = 3,
- }
- public enum LogonType : int
- {
- Interactive = 2,
- Network = 3,
- Batch = 4,
- Service = 5,
- Unlock = 7,
- NetworkCleartText = 8,
- NewCredentials = 9,
- }
- public enum LogonProvider : int
- {
- Default = 0,
- }
- internal class NativeCredMan
- {
- [DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
- //读与根据疑息
- static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag, out IntPtr CredentialPtr);
- [DllImport("Advapi32.dll", EntryPoint = "CredWriteW", CharSet = CharSet.Unicode, SetLastError = true)]
- //增长根据
- static extern bool CredWrite([In] ref NativeCredential userCredential, [In] UInt32 flags);
- [DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)]
- static extern bool CredFree([In] IntPtr cred);
- [DllImport("Advapi32.dll", EntryPoint = "CredDeleteW", CharSet = CharSet.Unicode)]
- //删除根据
- static extern bool CredDelete(string target, CRED_TYPE type, int flags);
- [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- public static extern bool CredEnumerate(string filter, uint flag, out uint count, out IntPtr pCredentials);
- //登岸考证
- [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- static extern bool LogonUser(string username, string domain,
- string password, LogonType logonType,
- LogonProvider logonProvider,
- out IntPtr userToken);
- [DllImport("Advapi32.DLL")]
- static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- private struct NativeCredential
- {
- public UInt32 Flags;
- public CRED_TYPE Type;
- public IntPtr TargetName;
- public IntPtr Comment;
- public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
- public UInt32 CredentialBlobSize;
- public IntPtr CredentialBlob;
- public UInt32 Persist;
- public UInt32 AttributeCount;
- public IntPtr Attributes;
- public IntPtr TargetAlias;
- public IntPtr UserName;
- internal static NativeCredential GetNativeCredential(Credential cred)
- {
- NativeCredential ncred = new NativeCredential();
- ncred.AttributeCount = 0;
- ncred.AttributeCount = 0;
- ncred.Attributes = IntPtr.Zero;
- ncred.Comment = IntPtr.Zero;
- ncred.TargetAlias = IntPtr.Zero;
- ncred.Type = cred.Type;
- ncred.Persist = (UInt32)cred.Persist;
- ncred.CredentialBlobSize = (UInt32)cred.CredentialBlobSize;
- ncred.TargetName = Marshal.StringToCoTaskMemUni(cred.TargetName);
- ncred.CredentialBlob = Marshal.StringToCoTaskMemUni(cred.CredentialBlob);
- ncred.UserName = Marshal.StringToCoTaskMemUni(cred.UserName);
- return ncred;
- }
- }
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct Credential
- {
- public UInt32 Flags;
- public CRED_TYPE Type;
- public string TargetName;
- public string Comment;
- public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
- public UInt32 CredentialBlobSize;
- public string CredentialBlob;
- public CRED_PERSIST Persist;
- public UInt32 AttributeCount;
- public IntPtr Attributes;
- public string TargetAlias;
- public string UserName;
- }
- /// <summary>
- /// 背增加计较机的根据办理此中增加根据
- /// </summary>
- /// <param name="key">internet地点大概收集地点</param>
- /// <param name="userName">用户名</param>
- /// <param name="secret">暗码</param>
- /// <param name="type">暗码范例</param>
- /// <param name="credPersist"></param>
- /// <returns></returns>
- public static int WriteCred(string key, string userName, string secret, CRED_TYPE type, CRED_PERSIST credPersist)
- {
- byte[] byteArray = Encoding.Unicode.GetBytes(secret);
- if (byteArray.Length > 512)
- throw new ArgumentOutOfRangeException("The secret message has exceeded 512 bytes.");
- Credential cred = new Credential();
- cred.TargetName = key;
- cred.CredentialBlob = secret;
- cred.CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(secret).Length;
- cred.AttributeCount = 0;
- cred.Attributes = IntPtr.Zero;
- cred.UserName = userName;
- cred.Comment = null;
- cred.TargetAlias = null;
- cred.Type = type;
- cred.Persist = credPersist;
- NativeCredential ncred = NativeCredential.GetNativeCredential(cred);
- bool written = CredWrite(ref ncred, 0);
- int lastError = Marshal.GetLastWin32Error();
- if (written)
- {
- return 0;
- }
- string message = string.Empty;
- if (lastError == 1312)
- {
- message = (string.Format(String.Format("Failed to save {0} with error code {{0}}.", key), lastError)
- + " This error typically occurrs on home editions of Windows XP and Vista. Verify the version of Windows is Pro/Business or higher.");
- }
- else
- {
- message = string.Format(String.Format("Failed to save {0} with error code {{0}}.", key), lastError);
- }
- return 1;
- }
- /// <summary>
- /// 读与根据
- /// </summary>
- /// <param name="targetName"></param>
- /// <param name="credType"></param>
- /// <param name="reservedFlag"></param>
- /// <param name="intPtr"></param>
- /// <returns></returns>
- public static bool WReadCred(string targetName, CRED_TYPE credType, int reservedFlag, out IntPtr intPtr)
- {
- bool flag = CredRead(targetName, CRED_TYPE.DOMAIN_PASSWORD, reservedFlag, out intPtr);
- return flag;
- }
- /// <summary>
- /// 删除根据
- /// </summary>
- /// <param name="target"></param>
- /// <param name="type"></param>
- /// <param name="flags"></param>
- /// <returns></returns>
- public static bool DeleteCred(string target, CRED_TYPE type, int flags)
- {
- return CredDelete(target, type, flags);
- }
- /// <summary>
- /// 登岸测试
- /// </summary>
- /// <param name="username"></param>
- /// <param name="domain"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public static bool LongonCheck(string username, string domain, string password)
- {
- IntPtr userToken = IntPtr.Zero;
- bool bImpersonated = LogonUser(username, domain, password, LogonType.NewCredentials, LogonProvider.Default, out userToken);
- try
- {
- if (bImpersonated)
- {
- if (!ImpersonateLoggedOnUser(userToken))
- {
- int nErrorCode = Marshal.GetLastWin32Error();
- CLRLogger.GetInstance().LogDevError("ImpersonateLoggedOnUser error;Code=" + nErrorCode);
- bImpersonated = false;
- }
- }
- }
- catch (Exception ex)
- {
- bImpersonated = false;
- CLRLogger.GetInstance().LogDevError("LongonCheck catch ex is " + ex.ToString());
- }
- return bImpersonated;
- }
- }
- /// <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 (Exception ex)
- {
- i = 1;
- CLRLogger.GetInstance().LogDevError("CreateCred has error : " + ex.Message);
- }
- return i == 0;
- }
- /// <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 (Exception ex)
- {
- flag = false;
- CLRLogger.GetInstance().LogDevError("QueryCred has error : " + ex.Message);
- }
- 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 (Exception ex)
- {
- flag = false;
- CLRLogger.GetInstance().LogDevError("DeleteCred has error : " + ex.Message);
- }
- return flag;
- }
- /// <summary>
- /// 登岸考证办法 IP为输出目次的地点,userName为账户名,domain为域名(united-imaging.com),password为暗码
- /// </summary>
- /// <param name="username"></param>
- /// <param name="domain"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public static bool LogonUser(string ip, string username, string domain, string password)
- {
- bool loggedOn = false;
- try
- {
- if (NativeCredMan.LongonCheck(username, domain, password) && Directory.Exists(ip))
- {
- loggedOn = true;
- }
- }
- catch (Exception ex)
- {
- loggedOn = false;
- CLRLogger.GetInstance().LogDevError("LogonUser has error : " + ex.Message);
- }
- return loggedOn;
- }
- }
复造代码 免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作! |
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
|