application_authenticaterequest

在.net中的global.asax中有application_authenticaterequest事件与application_beginrequest事件是在每次访问aspx文件都会触发.但是application_beginrequest中不能对已经经过froms身份验证的身份ticket票进行识别.所以只能放到application_authenticaterequest中去.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 域自动登录(AD自动登录)可以让用户在访问Web应用程序时无需再次输入他们的凭据。它允许用户在他们的计算机上通过Windows身份验证登录,并自动通过ASP.NET应用程序进行身份验证。 以下是实现ASP.NET域自动登录(AD自动登录)的步骤: 1. 在Web.config文件中启用Windows身份验证: ``` <authentication mode="Windows" /> ``` 2. 在Global.asax文件中添加以下代码: ``` protected void Application_AuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is WindowsIdentity) { // Get the Windows identity. var windowsIdentity = (WindowsIdentity)HttpContext.Current.User.Identity; // Get the user name and domain name. var userName = windowsIdentity.Name; var domainName = windowsIdentity.Name.Split('\\')[0]; // Authenticate the user against Active Directory. if (AuthenticateUser(userName, domainName)) { // Create a new identity using the user name and domain name. var identity = new GenericIdentity(userName, "Windows"); // Get the roles for the user from Active Directory. var roles = GetRolesForUser(userName, domainName); // Attach the roles to the identity. HttpContext.Current.User = new GenericPrincipal(identity, roles); } else { // Redirect the user to the login page. FormsAuthentication.RedirectToLoginPage(); } } } } } private bool AuthenticateUser(string userName, string domainName) { // Authenticate the user against Active Directory. // Return true if the user is authenticated, false otherwise. } private string[] GetRolesForUser(string userName, string domainName) { // Get the roles for the user from Active Directory. // Return an array of role names. } ``` 3. 在Web应用程序中创建一个登录页面。 4. 在登录页面中添加以下代码: ``` protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.IsAuthenticated) { // Redirect the user to the default page. Response.Redirect("~/Default.aspx"); } } } protected void btnLogin_Click(object sender, EventArgs e) { // Authenticate the user against Active Directory. if (AuthenticateUser(txtUserName.Text, txtPassword.Text)) { // Redirect the user to the default page. FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false); } else { // Display an error message. lblErrorMessage.Text = "Invalid username or password."; } } private bool AuthenticateUser(string userName, string password) { // Authenticate the user against Active Directory. // Return true if the user is authenticated, false otherwise. } ``` 这些步骤将帮助您实现ASP.NET域自动登录(AD自动登录)。当用户访问您的Web应用程序时,他们将通过Windows身份验证自动登录,并自动通过ASP.NET应用程序进行身份验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值