private void Login_Click(object sender, System.EventArgs e) { using (IdentityAnalogue ID = new IdentityAnalogue()) { if (ID.TryLogonAs(".", UserName.Text, Password.Text)) { string userName = UserName.Text; FormsAuthentication.SetAuthCookie(userName, false); string returnUrl; if (this.Request.Cookies[Global.ReturnUrl] == null) { returnUrl = this.Request.ApplicationPath; } else { returnUrl = this.Request.Cookies[Global.ReturnUrl].Value; } this.Response.Redirect(returnUrl); } else { lblMsg.Visible = true; lblMsg.Text = "*µÇ¼ʧ°Ü"; } } }
/// <summary> /// 以管理员身份运行相关代码 /// </summary> /// <param name="exec"></param> /// <returns></returns> public static bool RunAdminCode(ExecuteCode exec) { bool blnResult = false; using (IdentityAnalogue ID = new IdentityAnalogue()) { if (ID.TryLogonAs(".", ConfigurationManager.AppSettings["AnalogueID"], new SymmetricMethod().Decrypto(ConfigurationManager.AppSettings["AnaloguePWD"]))) { exec(); blnResult = true; } } return(blnResult); }
/// <summary> /// 测试原始用户名和密码是否正确 /// </summary> public static bool TestAuth(string username, string password) { #region Windows集成帐户认证 if (!IsApacheAuthMode()) { bool blnResult = false; using (IdentityAnalogue ID = new IdentityAnalogue()) { if (ID.TryLogonAs(".", username, password)) { blnResult = true; } } return(blnResult); } #endregion string url = ConfigurationManager.AppSettings["AuthURL4Pass"]; WebClient wc = new WebClient(); WebHeaderCollection headers = new WebHeaderCollection(); headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.8.1.19) Gecko/20081201 Firefox/2.0.0.19 (.NET CLR 3.5.30729)"); wc.Headers = headers; wc.Credentials = new NetworkCredential(username, password); //System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback( delegate(object o, X509Certificate cert, X509Chain chain, SslPolicyErrors errors) { return(true); }); try { MemoryStream ms = new MemoryStream(); using (Stream rms = wc.OpenRead(url)) { int bt = rms.ReadByte(); while (bt != -1) { ms.WriteByte(Convert.ToByte(bt)); bt = rms.ReadByte(); } rms.Close(); } //Console.WriteLine("读取响应流完成,输出响应头..."); //for (int i = 0; i < wc.ResponseHeaders.Count; i++) //{ // Console.WriteLine("{0}:{1}", wc.ResponseHeaders.AllKeys[i], wc.ResponseHeaders[i]); //} //Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray())); ms.Close(); ms.Dispose(); return(true); } catch (WebException) { return(false); } }
protected void Page_Load(object sender, EventArgs e) { #region 管理员配置密码加解密 if (Request["PwdGen"] != null) { Response.Write(new SymmetricMethod().Encrypto(Request["PwdGen"])); Response.End(); return; } else if (Request["PwdDen"] != null) { Response.Write(new SymmetricMethod().Decrypto(Request["PwdDen"])); Response.End(); return; } #endregion #region Test Code //StringBuilder sb = new StringBuilder(); //foreach (string s in Request.Form) //{ // sb.AppendFormat("{0}={1}\n", s, Request.Form[s]); //} //foreach (string s in Request.ServerVariables) //{ // sb.AppendFormat("{0}={1}\n",s, Request.ServerVariables[s]); //} ////Response.Write(sb.ToString()); //System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("debug.log")); //sw.Write(sb.ToString()); //sw.Close(); //sw.Dispose(); //Response.Write("0"); //Response.End(); #endregion string username = Request["username"]; string oldpwd = Request["oldpwd"]; string newpwd = Request["newpwd"]; string newpwdcfm = Request["newpwdcfm"]; string pwd = Request["pwd"]; string pwdcfm = Request["pwdcfm"]; if (username == null) { Response.Write("SVN-Auth Account API Page."); return; } #region 禁止修改用户名处理 if (("," + ConfigurationManager.AppSettings["DisabledModifyName"] + ",").ToLower() .Contains("," + username.ToLower() + ",")) { Response.Write("用户名[" + username + "]已被禁止在线修改!"); Response.End(); return; } #endregion bool ApacheAuthMode = AccountHelper.IsApacheAuthMode(); if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(oldpwd) && !string.IsNullOrEmpty(newpwd) && newpwd == newpwdcfm) { #region 修改密码 bool blnResult = false; if (!ApacheAuthMode) { using (IdentityAnalogue ID = new IdentityAnalogue()) { if (ID.TryLogonAs(".", username, oldpwd)) { blnResult = true; } } } else { //blnResult = TestAuth(username, oldpwd); //在修改时检查原始密码 blnResult = true; } if (blnResult == false) { Response.Write("用户密码不正确!"); } else { AccountHelper.RunAdminCode(new AccountHelper.ExecuteCode(delegate() { if (!ApacheAuthMode) { string currentUser = currentUser = Environment.MachineName + "/" + username; DirectoryEntry uEntry = new DirectoryEntry("WinNT://" + currentUser); try { uEntry.Invoke("SetPassword", newpwdcfm); uEntry.CommitChanges(); Response.Write("0"); } catch (Exception exp) { Response.Write("错误:" + exp.Message); } } else { string strChangeResult = AccountHelper.ApacheChangeOrCreateAccount(username, oldpwd, newpwdcfm); if (strChangeResult != "0") { Response.Write("错误:" + strChangeResult); } else { Response.Write("0"); } } })); } #endregion } else if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(pwd) && pwd == pwdcfm) { #region 创建账号 if (!ApacheAuthMode) { try { AccountHelper.RunAdminCode(new AccountHelper.ExecuteCode(delegate() { AccountHelper.CreateUserAccount(username, pwd, "", "Users", "[SVN-WinAuth]创建的用户"); })); Response.Write("0"); } catch (Exception exp) { Response.Write("错误:" + exp.Message); } } else { string strCreateResult = AccountHelper.ApacheChangeOrCreateAccount(username, null, pwd); if (strCreateResult != "0") { Response.Write("错误:" + strCreateResult); } else { Response.Write("0"); } } #endregion } else { Response.Write("请提供有效帐户信息!"); } Response.End(); }