2016-09-01 1 views
0

시나리오 : 웹용 Visual Studio Express 2012가 있습니다. 아무도 인증되지 않으면 내 웹 사이트의 로그 아웃 기능을 테스트하고 있습니다 (게스트 모드, 말하자면).HttpContext.Current.User.Identity .: ID가 로컬 웹 로그인과 웹 로그인을 연결합니다.

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

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

<퍼센트 - 프레임 워크 스크립트 - %> <퍼센트 - 사이트 스크립트 - %> <% ---- %>

 </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 로그인이 인증 된 사용자로 표시됩니다. 나는 응용 프로그램 내에서만 인증 된 사용자를 결정할 수 있기를 원합니다. 이것을 어떻게 성취합니까? 로 작동합니다

+1

대부분 사이트에서 Windows 인증을 사용 중이므로 결과 요청이 자동으로 인증됩니다. [편집] web.config의 해당 부분을 표시하여 다른 사람이 귀하의 질문에 답변 할 수 있도록 도와주십시오. –

+0

Alexei의 제안에 따라 web.config 코드가 추가되었습니다. – MDDeVane

+0

손님에게 Default.aspx 페이지로 이동하여 로그인 할 수있는 권한을 부여하고 싶습니다. aspx. 다른 웹 페이지에 액세스하는 메뉴를 숨겨서이 작업을 수행 할 수는 있지만 URL의 하위 도메인에 입력하는 것을 막지는 못합니다. – MDDeVane

답변

0

일반적인 ASP.NET 웹 폼 인증 방식은 다음과 : 사용자가 성공적으로 서명

  1. 후, 웹 사이트가 인증 된 사용자 또는 세션을 나타내는 쿠키를 설정합니다. ASP.NET은 forms authentication ticket and cookie을 제공하거나 사용자가 직접 롤업 할 수 있습니다.

  2. 각각의 모든 HTTP 요청은 쿠키/파싱 해독하는 HttpModule으로합니다 (Authenticate 이벤트를 통해) 차단 및 현재 사용자를 나타내는 객체 HttpContext.User 설정된다. 당신이 발견 한 것처럼, 이것은 그 자체로는 일어나지 않습니다. 사용자 고유의 모듈을 롤하거나 Microsoft's

  3. 을 사용하고 응용 프로그램 사용자 만 HttpContext.User 속성을 사용하여 현재 사용자의 ID를 확인할 수 있습니다. 1 & 2 단계를 건너 뛴 경우 HttpContext.User 속성은 App Pool (이 경우 사용자)이라는 실행 컨텍스트에 할당 된 사용자의 ID를 반환합니다. 이는 사이트를 로컬로 실행하고 있기 때문일 수 있습니다.

여기에는 기본 제공 ASP.NET 폼 인증을 사용하여 cookbook이 있습니다. 또한 새 프로젝트 마법사를 사용하여 양식 인증을 사용하는 웹 사이트를 생성 할 수 있어야합니다.

+0

나는 무의식적으로 보안 측면에서 회원 자격과 정체성을 모두 사용하려고 노력하고 있음을 발견했습니다. 나는이 기사가 지적한 차이점을 발견했다. 간단히 말해서, 저는 회원 자격이 필요한 것일 뿐이니까 양식 방법이 필요하다고 생각하지 않습니다. https://brockallen.com/2012/09/02/think-twice-about-using-membershipprovider-and-simplemembership/ . – MDDeVane

관련 문제