首页 > 编程 > .NET > 正文

在ASP.Net 中使用实现windows登陆的方法

2024-07-10 13:05:18
字体:
来源:转载
供稿:网友
public bool login(string username, string password, string domain)
{
string text1 = domain.trim();
string text2 = username.trim();
text2 = text2.replace("/", @"/");
int num1 = text2.indexof('//');
if (num1 != -1)
{
text1 = text2.substring(0, num1);
text2 = text2.substring(num1 + 1);
}
else
{
num1 = text2.indexof('@');
if (num1 != -1)
{
text1 = text2.substring(num1 + 1);
text2 = text2.substring(0, num1);
}
}
return this.authenticateuser(text2, password.trim(), text1);
}

private bool authenticateuser(string username, string password, string domain)
{
bool flag1 = false;
try
{
int num1;
intptr ptr1;
if (!windowsauth.logonuser(username, domain, password, 2, 0, out num1))
{
return flag1;
}
ptr1 = new intptr(num1);
windowsidentity identity1 = new windowsidentity(ptr1);
windowsprincipal principal1 = new windowsprincipal(identity1);
httpcontext.current.user = principal1;
formsauthentication.setauthcookie(principal1.identity.name, false);
formsauthentication.redirectfromloginpage(username, false);
flag1 = true;
}
catch (exception)
{
}
return flag1;
}


[dllimport("advapi32.dll")]
public static extern bool logonuser(string lpszusername, string lpszdomain, string lpszpassword, int dwlogontype, int dwlogonprovider, out int phtoken);



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表