2016-04-22 82 views
0

我正在創建ASP.NET應用程序。我只需要在default.aspx頁面中向匿名用戶顯示兩個頁面(login.aspx和register.aspx)。允許匿名用戶僅訪問ASP.NET中的登錄和註冊頁面

我試過Allow anonymus users to access only Login and Register page,但不知道我自己的代碼在哪裏完全改變。那麼有什麼提示?

這是例如在我的應用程序:

example

的Web.config

<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <remove name="CS" /> 
    <add name="CS" connectionString="Data Source=NET134;User ID=admin;Password=admin123;Initial Catalog=Reservations;" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <system.web> 
    <authentication mode="Forms"> 

     <!-- Redirect LonginStatus Loginurl to Account/login.aspx --> 

     <forms loginUrl="Account/Login.aspx" /> 

    </authentication> 

    <compilation debug="true" targetFramework="4.5.2"> 
     <assemblies> 
     <add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /> 
     <add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
     </assemblies> 
     <buildProviders> 
     <add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" /> 
     </buildProviders> 
    </compilation> 
    <httpRuntime targetFramework="4.5.2" /> 
    <pages> 
     <namespaces> 
     <add namespace="System.Web.Optimization" /> 
     <add namespace="Microsoft.AspNet.Identity" /> 
     </namespaces> 
     <controls> 
     <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" /> 
     <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" /></controls> 
    </pages> 
    <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15"> 
     <providers> 
     <clear /> 
     <add 
      name="SqlProvider" 
      type="System.Web.Security.SqlMembershipProvider" 
      connectionStringName="CS" 
      applicationName="MyApplication" 
      enablePasswordRetrieval="false" 
      enablePasswordReset="true" 
      requiresQuestionAndAnswer="true" 
      requiresUniqueEmail="true" 
      passwordFormat="Hashed" /> 
     </providers> 
    </membership> 
    <profile> 
     <providers> 
     <!-- 
     ASP.NET Membership Profile is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template 
     --> 
     <clear /> 
     </providers> 
    </profile> 
    <roleManager> 
     <!-- 
      ASP.NET Membership Role is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template 
     --> 
     <providers> 
     <clear /> 
     </providers> 
    </roleManager> 
    <!-- 
      If you are deploying to a cloud environment that has multiple web server instances, 
      you should change session state mode from "InProc" to "Custom". In addition, 
      change the connection string named "DefaultConnection" to connect to an instance 
      of SQL Server (including SQL Azure and SQL Compact) instead of to SQL Server Express. 
     --> 
    <sessionState mode="InProc" customProvider="DefaultSessionProvider"> 
     <providers> 
     <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" /> 
     </providers> 
    </sessionState> 
    </system.web> 
    <system.webServer> 
    <modules> 
     <remove name="FormsAuthentication" /> 
    </modules> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.OAuth" culture="neutral" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.Cookies" culture="neutral" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
</configuration> 

回答

1

您可以嘗試2路 首先是 - 。第一時間化妝菜單上可見的僞。 。用戶登錄後,將該用戶添加到會話中。 。然後添加條件,如果會話不爲空,然後使菜單可見真

第二種方式是用戶登錄後,您可以從數據庫創建動態菜單。

也可以嘗試使用Web.config文件參考以下鏈接:

http://weblogs.asp.net/gurusarkar/setting-authorization-rules-for-a-particular-page-or-folder-in-web-config

+0

這些想法都很好,我會試試看。 – Paulius

0

你可以在一個單獨的母版頁中的所有其他頁面和主頁將有在.cs文件中檢查用戶是否按會話進行了登錄,並且允許匿名人員訪問的頁面不會與該主頁面一起使用。 例如:

if (Session["IsLog"] == null) 
    { 
     Response.Redirect("Error.aspx"); 
    } 
    if (Session["IsLog"].ToString() != "true") 
    { 
     Response.Redirect("Error.aspx"); 
    } 
相關問題