简体   繁体   English

HttpContext.Current.User.Identity .:身份正在从我的本地Windows登录名与我正在测试的网站登录名中提取

[英]HttpContext.Current.User.Identity.: Identity is pulling rom my local Windows login vs the website login I'm testing

Scenario: I have Visual Studio Express 2012 for Web. 场景:我有用于Web的Visual Studio Express 2012。 I am testing the Logout functionality of my website when no one is authenticated (Guest mode, so to speak). 没有人通过身份验证时,我正在测试我的网站的注销功能(可以说是访客模式)。

site.master.cs code 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;
    }
}

site.master code site.master代码

    <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>

<%--Framework scripts--%> <%--Site scripts--%> <%----%> <%-框架脚本-%> <%-站点脚本-%> <%----%>

        </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 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>

RESULT: My Windows login is showing up as being the authenticated user, when that is not my intent. 结果:如果不是我的意图,我的Windows登录名将显示为经过身份验证的用户。 I want to be able to determine authenticated users within the application only. 我只希望能够确定应用程序内经过身份验证的用户。 How do I achieve this? 我该如何实现?

A typical ASP.NET web forms authentication scheme will work as follows: 典型的ASP.NET Web表单身份验证方案将按以下方式工作:

  1. After the user successfully signs on, the web site sets a cookie representing the authenticated user or session. 用户成功登录后,网站将设置一个cookie,该cookie代表已通过身份验证的用户或会话。 ASP.NET offers the forms authentication ticket and cookie or you can roll your own. ASP.NET提供表单身份验证票和cookie,或者您可以自己滚动。

  2. Each and every HTTP request is intercepted (via the Authenticate event) by an HttpModule to parse/decrypt the cookie and set HttpContext.User to an object that represents the current user. HttpModule截获每个HTTP请求(通过Authenticate事件),以解析/解密cookie并将HttpContext.User设置为代表当前用户的对象。 As you have discovered, this does not happen by itself. 正如您所发现的,这并不是单独发生的。 You can roll your own module, or use Microsoft's 您可以滚动自己的模块,也可以使用Microsoft的

  3. Then and only then can your application user the HttpContext.User property to determine the current user's identity. 然后只有这样,您的应用程序用户才能使用HttpContext.User属性确定当前用户的身份。 If you skip steps 1 & 2, the HttpContext.User property returns the identity of the user assigned to the execution context of the App Pool , which in this case is you, probably because you are running the site locally. 如果跳过第1步和第2步,则HttpContext.User属性将返回分配给App Pool的执行上下文的用户的身份,在本例中为您,这可能是因为您是在本地运行站点。

Here is a cookbook for doing it with built-in ASP.NET forms authentication. 这是一使用内置ASP.NET表单身份验证进行操作的食谱 You should also be able to generate a web site that uses forms authentication by using the new project wizard. 您还应该能够通过使用新的项目向导来生成一个使用表单身份验证的网站。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用formsauthentication进行登录并使用HttpContext.Current.User.Identity - Using formsauthentication for login and use HttpContext.Current.User.Identity 登录身份-在HttpContext中不起作用 - Login Identity - Not working in HttpContext 登录控件LoggedIn事件中的HttpContext.Current.User.Identity.IsAuthenticated为false - HttpContext.Current.User.Identity.IsAuthenticated is false in Login control LoggedIn event 如何使用HttpContext.Current.User.Identity为SmtpClient.Credentials提供身份验证? - How can I use the HttpContext.Current.User.Identity to provide authentication to my SmtpClient.Credentials? 模拟HttpContext.Current.User.Identity.Name - Mock HttpContext.Current.User.Identity.Name 更改HttpContext.Current.User.Identity.Name - Changing HttpContext.Current.User.Identity.Name HttpContext.Current.User.Identity.Name 为空,Form.authentication 在 IIS 的网站子文件夹中 - HttpContext.Current.User.Identity.Name is empty with Form.authentication in subfolder of website in IIS ASP.NET中的System.Web.HttpContext.Current.User.Identity.Name与System.Environment.UserName的对比 - System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET 在具有Windows身份验证模式的MVC4中,始终HttpContext.Current.User.Identity.IsAuthenticated = false - Always HttpContext.Current.User.Identity.IsAuthenticated = false in MVC4 with windows authentication mode and not working correctly MVC应用程序身份验证设置为Windows。 Httpcontext.current.user.identity.name空字符串 - MVC application authentication set to Windows. Httpcontext.current.user.identity.name empty string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM