HttpContext.Current.User.Identity.:身份是从我的本地Windows登录与我正在测试的网

本文关键字:登录 Windows 测试 Identity User Current 身份 我的 HttpContext | 更新日期: 2023-09-27 18:05:25

场景:我有Visual Studio Express 2012 for Web。我正在测试我的网站登出功能时,没有人被认证(Guest模式,可以这么说)。

site.master.cs代码

    protected void Page_Load(object sender, EventArgs e)
{
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
        TopBox.Text = "Welcome " + HttpContext.Current.User.Identity.Name;
        RegisterLabel.Visible = false;
        Menu1.Visible = true;
        Menu2.Visible = false;
    }
    else
    {
        TopBox.Text = "Welcome, Guest.";
        RegisterLabel.Visible = true;
        Menu1.Visible = false;
        Menu2.Visible = true;
    }
}

网站。主代码

    <script type="text/javascript">
    var t;
    window.onload = resetTimer;
    document.onmousemove = resetTimer;
    document.onkeypress = resetTimer;
    function logout() {
        try {
            document.execCommand("ClearAuthenticationCache");
            window.location.href = window.location.protocol.replace(/':/g, '') + "://" + window.location.host + "Account/Login.aspx";
        }
        catch (e) { }
    }
    function resetTimer() {
        window.clearTimeout(t);
        t = window.setTimeout(logout, 900000);
    }
</script>

& lt; %——框架脚本——%>& lt; %——网站脚本——%>& lt ;%----%>

        </Scripts>
    </asp:ScriptManager>

    <header>
        <div class="content-wrapper">
            <p style="float: left">
                <br />
            </p>
            <br />
            <asp:Label ID="RegisterLabel" runat="server" Text="Register">
                           <a style="font-size:small; color:white; align-content:center" href="Account/Register.aspx" >
                               Not Registered? Sign Up
                           </a>
            </asp:Label>
            <br />
            <div>
                <p>
                    <a class="MasterMenu" style="color: #A90000; font-size: larger" runat="server" href="~/">Following High School Football</a>
                </p>
                <p>
                    <asp:Label ID="TopBox" Font-Size="Small" Text="" runat="server" TextMode="SingleLine"></asp:Label>
                </p>
                <asp:Menu ID="Menu2" class="dropmenu" runat="server" Style="margin-left: 25%; margin-right: 25%; font-size: 12px;"
                    BackColor="Transparent"
                    StaticDisplayLevels="2" StaticHoverStyle-BackColor="ForestGreen" StaticMenuItemStyle-HorizontalPadding="1px"
                    StaticMenuItemStyle-ItemSpacing="1px" StaticMenuItemStyle-Width="100%" Font-Size="Large"
                    StaticMenuItemStyle-BackColor="DarkGreen" StaticMenuItemStyle-Height="40px">
                    <Items>
                        <asp:MenuItem NavigateUrl="Account/Login.aspx" Text="Login" Value="Login"></asp:MenuItem>
                        <asp:MenuItem NavigateUrl="Account/Register.aspx" Text="Register" Value="Register"></asp:MenuItem>
                        <asp:MenuItem NavigateUrl="Account/Recovery.aspx" Text="Password" Value="Recovery"></asp:MenuItem>
                    </Items>
                </asp:Menu>
                <asp:Menu ID="Menu1" class="dropmenu" runat="server" Style="margin-left: 25%; margin-right: 25%; font-size: 12px;"
                    BackColor="Transparent"
                    StaticDisplayLevels="2" StaticHoverStyle-BackColor="ForestGreen" StaticMenuItemStyle-HorizontalPadding="1px"
                    StaticMenuItemStyle-ItemSpacing="1px" StaticMenuItemStyle-Width="100%" Font-Size="Large"
                    StaticMenuItemStyle-BackColor="DarkGreen" StaticMenuItemStyle-Height="40px">
                    <Items>
                        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home" Value="Home"></asp:MenuItem>
                        <asp:MenuItem NavigateUrl="~/Records.aspx" Text="Records" Value="Records"></asp:MenuItem>
                        <asp:MenuItem NavigateUrl="~/Pickem.aspx" Text="Pick Em" Value="Pick Em"></asp:MenuItem>
                        <asp:MenuItem NavigateUrl="#" Text="Favorites" Value="Favorites"></asp:MenuItem>
                        <asp:MenuItem NavigateUrl="Account/Logoff.aspx" Text="Logoff" Value="Logoff"></asp:MenuItem>
                    </Items>
                </asp:Menu>
            </div>
        </div>
    </header>
    <div id="body">
        <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
        <section class="content-wrapper main-content clear-fix">
            <asp:ContentPlaceHolder runat="server" ID="MainContent" />
        </section>
    </div>
    <footer style="position: absolute; bottom: -240px; width: 98%; left: 136px; height: 63px;">
        <div class="content-wrapper">
            <div class="float-right">
                <br />
                <br />
                <p>&copy; <%: DateTime.Now.Year %> - FHSF </p>
            </div>
        </div>
    </footer>
</form>

web . config

      <system.web>
    <compilation debug="true">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Web.Extensions.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5"/>
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization"/>
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
      </controls>
    </pages>
    <anonymousIdentification enabled="true"></anonymousIdentification>
    <profile defaultProvider="DefaultProfileProvider" enabled="true">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="SqlServicesConnection" applicationName="/"/>
      </providers>
      <properties>
          <add name="UserDetailID" type="int" />
      </properties>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="SqlServicesConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
      </providers>
    </membership>
    <roleManager defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="SqlServicesConnection" applicationName="/"/>
      </providers>
    </roleManager>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="SqlServicesConnection"/>
      </providers>
    </sessionState>
  </system.web>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0"/>
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0"/>
      </dependentAssembly>
      <dependantAssembly>
        <assemblyIdentity name='EntityFramework' culture="neutral" PublicKeyToken="b77a5c561934e089" />
        <bindingRedirect  oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0"/>
      </dependantAssembly> 
    </assemblyBinding>
  </runtime>
</configuration>

结果:我的Windows登录显示为经过身份验证的用户,当这不是我的意图。我希望能够仅在应用程序中确定经过身份验证的用户。我该如何做到这一点?

HttpContext.Current.User.Identity.:身份是从我的本地Windows登录与我正在测试的网

一个典型的ASP。. NET web表单认证方案的工作原理如下:

  1. 用户登录成功后,网站设置一个cookie,代表该用户或会话的身份验证。ASP。. NET提供了表单身份验证票据和cookie,或者您可以自己创建。

  2. 每个HTTP请求都被HttpModule拦截(通过Authenticate事件)来解析/解密cookie并设置HttpContext。User转换为表示当前用户的对象。正如您所发现的,这不会自动发生。你可以滚动你自己的模块,或者使用微软的

  3. 只有这样你的应用用户才能使用HttpContext。User属性以确定当前用户的标识。如果你跳过步骤1 &2、HttpContext。User属性返回分配给App Pool执行上下文的用户的身份,在本例中是你,可能是因为你在本地运行站点。

这是一本使用内置ASP的烹饪书。. NET表单验证。您还应该能够通过使用新的项目向导生成使用表单身份验证的网站。