Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ASP.NET页面中获取Windows登录_C#_Asp.net_Login_Windows Identity - Fatal编程技术网

C# 从ASP.NET页面中获取Windows登录

C# 从ASP.NET页面中获取Windows登录,c#,asp.net,login,windows-identity,C#,Asp.net,Login,Windows Identity,就像标题所说的那样。需要asp.net页面中的windows登录和域信息 我试过了 string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 但它返回的是IIS应用程序池,而不是用户名 谢谢尝试,只需作为用户从代码背后访问即可。它返回域和用户名,但应该很容易根据您的需要进行调整。这在过去对我很有效。如果需要,您还可以使用它来管理应用程序中的角色 编辑 下面是my web.config的相关部分

就像标题所说的那样。需要asp.net页面中的windows登录和域信息

我试过了

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
但它返回的是IIS应用程序池,而不是用户名

谢谢

尝试,只需作为
用户
从代码背后访问即可。它返回域和用户名,但应该很容易根据您的需要进行调整。这在过去对我很有效。如果需要,您还可以使用它来管理应用程序中的角色

编辑
下面是my web.config的相关部分。我还用于在数据库中设置角色管理器所需的表。然后我可以使用
User.Identity.Name
User.Identity.IsInRole

<connectionStrings>
    <clear/>
    <add name="SqlRoleManagerConnection"
         connectionString="myConnectionString">
    </add>
  </connectionStrings>

  <system.web>
    <authentication mode="Windows" />
    <authorization>
      <deny users="?"/>
    </authorization>
    <roleManager enabled="true" defaultProvider="SqlRoleManager">
      <providers>
        <clear/>
        <add name="SqlRoleManager"
             type="System.Web.Security.SqlRoleProvider"
             connectionStringName="SqlRoleManagerConnection"
             applicationName="myAppName" />
      </providers>
    </roleManager>
  </system.web>

试试,只需作为
用户
从代码背后访问即可。它返回域和用户名,但应该很容易根据您的需要进行调整。这在过去对我很有效。如果需要,您还可以使用它来管理应用程序中的角色

编辑
下面是my web.config的相关部分。我还用于在数据库中设置角色管理器所需的表。然后我可以使用
User.Identity.Name
User.Identity.IsInRole

<connectionStrings>
    <clear/>
    <add name="SqlRoleManagerConnection"
         connectionString="myConnectionString">
    </add>
  </connectionStrings>

  <system.web>
    <authentication mode="Windows" />
    <authorization>
      <deny users="?"/>
    </authorization>
    <roleManager enabled="true" defaultProvider="SqlRoleManager">
      <providers>
        <clear/>
        <add name="SqlRoleManager"
             type="System.Web.Security.SqlRoleProvider"
             connectionStringName="SqlRoleManagerConnection"
             applicationName="myAppName" />
      </providers>
    </roleManager>
  </system.web>

这是我在VB.net中的代码

var strUser=System.Web.HttpContext.Current.User.Identity.Name

因此,C#必须符合:(未测试) 字符串strUser=System.Web.HttpContext.Current.User.Identity.Name

在Web.Config文件中

<configuration>
    <system.web>
        <authentication mode="Windows"/>
        <identity impersonate="true" />
    </system.web>
</configuration>

这是我在VB.net中的代码

var strUser=System.Web.HttpContext.Current.User.Identity.Name

因此,C#必须符合:(未测试) 字符串strUser=System.Web.HttpContext.Current.User.Identity.Name

在Web.Config文件中

<configuration>
    <system.web>
        <authentication mode="Windows"/>
        <identity impersonate="true" />
    </system.web>
</configuration>


HTH可能是这样吗?我在我的web.comf中的何处设置“”?另外,我做了一个测试,验证的结果是错误的。嗯,也许是这个?我在我的web.comf中的何处设置“”?另外,我做了一个测试,IsAuthenticated为False。为了使其工作,您需要在IIS或web.config中设置应用程序,以不允许匿名用户,否则将得到一个空白。@Tim请参阅编辑,添加了web.config的相关部分为了使其工作,您需要在IIS或web.config中设置应用程序,以不允许匿名用户,否则将得到一个空白。@Tim请参阅编辑,添加了web.config的相关部分