适用范围:不想使用WINDOWS弹出框,在同一域内进行WINDOWS认证登录
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
namespace DomainLogon
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox DomainUser;
protected System.Web.UI.WebControls.TextBox Password;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//先进行其它的验证
Page.RegisterClientScriptBlock("LogonScript",CombineScript("http://localhost:81/sites/BaxterBPM/default.aspx"/*改成您自己的SPS首页面*/,DomainUser.Text.Replace("//","") ,Password.Text));
Page.RegisterClientScriptBlock("DoLogon","<script>sendRequest();</script>");
}
private string CombineScript(string logonUrl,string domainUser,string password)
{
StringBuilder sb = new StringBuilder();
sb.Append(@"
<script language=""javascript"">
function sendRequest()
{
var sUsername = """+domainUser +@""";
var sPassword = """+password+@""";
var sCredUrl = """+logonUrl+@""";
try
{
var xmlHttp = new ActiveXObject(""MSXML2.XMLHTTP"");
xmlHttp.open(""GET"", sCredUrl, false, sUsername, sPassword);
xmlHttp.send();
if (xmlHttp.status != 200)
{
alert(""Logon failed"");
return;
}
}
catch (exception)
{
alert(exception.message);
return;
}
// setCookie(""_Username"", sUsername);
// setCookie(""_Password"", sPassword);
location.href = sCredUrl;
}
// function setCookie(sName, sValue)
// {
// document.cookie = sName + ""="" + escape(sValue) + ""; domain=192.168.1.105:81"";
// }
</script>");
return sb.ToString();
}
}
}