回答

0

Application_Start()在请求ASP.NET应用程序中的第一个资源(如页面)时被调用。另外,在应用程序的生命周期中它只被调用一次。据说,在那个时候,你将无法授权所有的用户。客户端计算机上的当前Windows用户信息由Web浏览器通过密码交换提供,涉及与Web服务器 [Wiki]进行哈希处理。只有这样你才能授权用户。因此,User.Identity.IsAuthenticated应该在页面加载时工作(请参阅下面的代码)。您可以提取你需要的常用方法的逻辑,并调用它的第一次加载页面

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (User.Identity.IsAuthenticated) 
    { 
     Page.Title = "Home page for " + User.Identity.Name; 
     // add your logic here 
    } 
    else 
    { 
     // you get here if auth failed. 
     Page.Title = "Home page for guest user."; 
    } 
} 

更多信息的更新后:

我会使用的Application_Start()检查和如果它们不存在,则添加角色。

if (! Roles.RoleExists("Auditors")) 
      Roles.CreateRole("Auditors"); 

您可能会发现这篇文章有用:http://weblogs.asp.net/scottgu/Recipe_3A00_-Implementing-Role_2D00_Based-Security-with-ASP.NET-2.0-using-Windows-Authentication-and-SQL-Server

+0

您好,我想是这样的:保护无效Application_AuthenticateRequest(对象发件人,EventArgs的){ 如果 (HttpContext.Current.User!= NULL){ 如果 (HttpContext.Current。 (HttpContext.Current.User.Identity是WindowsIdentity) var amduser = new UsuarioDB();如果(HttpContext.Current.User.Identity是WindowsIdentity) var amduser = new UsuarioDB();如果(HttpContext.Current.User.Identity是WindowsIdentity) amduser.DefinirPropriedadesUsuario(); } } } }但Current.User.Identity始终为空 – 2014-10-30 12:26:28

+0

@ user1200656,可以试试吗? http://stackoverflow.com/questions/1663535/httpcontext-current-user-is-null-even-though-windows-authentication-is-on – 2014-10-30 15:44:12

1

我用这个:

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) 
      { 
       if (string.IsNullOrEmpty(Usuario.Nome)) 
       { 

确保,在你的项目性质(ALT项目+输入)点击WEB检查NTLM AUTHENTICATION。

这对我有效。现在HttpContext.Current.User.Identity不再为空

+0

微软不再在应用程序中推荐NTLM – 2014-10-30 16:10:31

+0

这是它工作的唯一方式,所有其他方式User.Identity.IsAuthenticated是null当从Visual Studio – 2014-10-30 16:22:36

+0

运行应用程序你可以试试这个吗? http://stackoverflow.com/questions/1663535/httpcontext-current-user-is-null-even-though-windows-authentication-is-on – 2014-10-30 16:26:09

相关问题