C#客户端实现域环境内Windows认证免密码自动身份认证登录
C#客户端实现域环境内Windows认证免密码自动身份认证登录
客户端代码:
/// <summary> /// 认证客户端 /// </summary> public class AuthClient { /// <summary> /// 认证当前用户 /// </summary> /// <param name="authServerUrl">服务端url</param> /// <returns>认证结果信息</returns> public AuthResult AuthUser(string authServerUrl) { string strResponse = string.Empty; AuthResult result = new AuthResult(); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(authServerUrl); //request.UseDefaultCredentials = true; //request.PreAuthenticate = true; request.Credentials = CredentialCache.DefaultCredentials; //request.Credentials = CredentialCache.DefaultNetworkCredentials; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { strResponse = sr.ReadToEnd(); } } //HttpClientHandler handler = new HttpClientHandler() //{ // Credentials = CredentialCache.DefaultCredentials //}; //HttpClient client = new HttpClient(handler); //strResponse = client.GetStringAsync(url); } catch(Exception e) { result.ErrCode = 1; result.ErrMessage = $"访问接口失败,{e.Message + e.StackTrace}"; return result; } JavaScriptSerializer serializer = new JavaScriptSerializer(); if(!string.IsNullOrWhiteSpace(strResponse)) { try { result = serializer.Deserialize<AuthResult>(strResponse); return result; } catch(Exception e1) { result.ErrCode = 2; result.ErrMessage = $"数据转换失败,result:{strResponse},{e1.Message + e1.StackTrace}"; return result; } } result.ErrCode = 2; result.ErrMessage = $"接口返回了空数据,result:{strResponse}"; return result; } }
服务端:在IIS上部署相关web站点,设置windows身份验证证,禁用匿名身份验证