ASP.net Application 中使用域用户登录

转自:http://weblogs.3322.org/   
现在做的一个程序中要求ASP.net 程序可以使用已经存在的域用户来登录(而且为了与其它程序界面一致一定要使用 Forms 登录),查找了一些相关的资料发现还是可以实现的。

   主要还是依靠 advapi32.dll 中的 LogonUser API 函数。 

ASP.net Application 中使用域用户登录using System.Web.Security;
ASP.net Application 中使用域用户登录
using System.Runtime.InteropServices;
ASP.net Application 中使用域用户登录
ASP.net Application 中使用域用户登录[DllImport(
"advapi32.dll", CharSet=CharSet.Auto)]
ASP.net Application 中使用域用户登录
public static extern int LogonUser(String lpszUserName,
ASP.net Application 中使用域用户登录String lpszDomain,
ASP.net Application 中使用域用户登录String lpszPassword,
ASP.net Application 中使用域用户登录
int dwLogonType,
ASP.net Application 中使用域用户登录
int dwLogonProvider,
ASP.net Application 中使用域用户登录
ref IntPtr phToken);
ASP.net Application 中使用域用户登录
ASP.net Application 中使用域用户登录
public const int LOGON32_LOGON_INTERACTIVE = 2;
ASP.net Application 中使用域用户登录
public const int LOGON32_PROVIDER_DEFAULT = 0;
ASP.net Application 中使用域用户登录
ASP.net Application 中使用域用户登录
void Login_Click(Object sender, EventArgs E)
ASP.net Application 中使用域用户登录ASP.net Application 中使用域用户登录
ASP.net Application 中使用域用户登录{
ASP.net Application 中使用域用户登录IntPtr token 
= IntPtr.Zero;
ASP.net Application 中使用域用户登录
ASP.net Application 中使用域用户登录
if(LogonUser(UserName.Value,
ASP.net Application 中使用域用户登录UserDomain.Value,
ASP.net Application 中使用域用户登录UserPass.Value,
ASP.net Application 中使用域用户登录LOGON32_LOGON_INTERACTIVE,
ASP.net Application 中使用域用户登录LOGON32_PROVIDER_DEFAULT,
ASP.net Application 中使用域用户登录
ref token) != 0)
ASP.net Application 中使用域用户登录ASP.net Application 中使用域用户登录
ASP.net Application 中使用域用户登录{
ASP.net Application 中使用域用户登录FormsAuthentication.RedirectFromLoginPage(UserName.Value,
ASP.net Application 中使用域用户登录PersistCookie.Checked);
ASP.net Application 中使用域用户登录}

ASP.net Application 中使用域用户登录
else
ASP.net Application 中使用域用户登录ASP.net Application 中使用域用户登录
ASP.net Application 中使用域用户登录{
ASP.net Application 中使用域用户登录lblResults.Text 
= "Invalid Credentials: Please try again";
ASP.net Application 中使用域用户登录}

ASP.net Application 中使用域用户登录}

ASP.net Application 中使用域用户登录


   其它方面的使用与普通的forms 程序没有太大的区别,也许还有更好的方法。

附注:技术的连贯性体现

上一篇:《iOS 6高级开发手册(第4版)》——1.2节添加设备能力限制


下一篇:T-SQL HASHBYTES MD5加密