asp.net mvc中要允许部分视图不用登陆验证,怎么设置?

sibweo0916 2014-09-04 01:59:52
我在web.config中设置类似
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="~/home/login" protection="All" path="/" timeout="20"/>
</authentication>
<authorization>
<deny users="?"></deny>
</authorization>

以后所有的视图都要登陆才能访问了,要允许部分视图不用登陆就可以访问,该怎么设置。
要允许部分控制器(该控制器不返回视图,只处理数据)不用登陆就可以访问,怎么设置??
...全文
483 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sibweo0916 2014-09-04
  • 打赏
  • 举报
回复
我把它贴出来,你看看是什么原因?
<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<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=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="WeixinVerifyEntities" connectionString="metadata=res://*/Models.WeixinVerify.csdl|res://*/Models.WeixinVerify.ssdl|res://*/Models.WeixinVerify.msl;provider=System.Data.SqlClient;provider connection string="data source=10.14.2.63;initial catalog=WeixinVerify;user id=sa;password=Pass@word!;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="SystemName" value="xxx系统" />
    <add key="isdev" value="true"/>
    <add key="SystemID" value="50058"/>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="DefaultConnectionString" value="true" />
    <add key="VerifyToken" value="transferverifydata" />
  </appSettings>
  
  

  <system.web>
      <compilation debug="true" targetFramework="4.0" />

      <!--登陆配置->
      <authentication mode="Forms">
        <forms loginUrl="~/Account/LogOn" timeout="2880" />
      </authentication>
      <authorization>
        <deny users="?"/>
        <allow users="*"/>
      </authorization>
      <!--配置完成-->


      <pages>
        <namespaces>
          <add namespace="System.Web.Helpers" />
          <add namespace="System.Web.Mvc" />
          <add namespace="System.Web.Mvc.Ajax" />
          <add namespace="System.Web.Mvc.Html" />
          <add namespace="System.Web.Optimization" />
          <add namespace="System.Web.Routing" />
          <add namespace="System.Web.WebPages" />
        </namespaces>
      </pages>
      <profile defaultProvider="DefaultProfileProvider">
        <providers>
          <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
        </providers>
      </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="DefaultConnection" 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="DefaultConnection" applicationName="/" />
        </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=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
        </providers>
      </sessionState>
    </system.web>
  
  <location path="~/Home/Nocheck1">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>

  <location path="~/Home/Nocheck2">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>
ayanamireizero 2014-09-04
  • 打赏
  • 举报
回复
<system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" maxRequestLength="1048576" /> <authentication mode="Forms"> <forms name="Admin" loginUrl="~/Ajax/CheckLogin" timeout="2880" /> </authentication> <customErrors mode="Off" /> </system.web> <location path="1"> <system.web> <authorization> <allow users="admin" /> <deny users="?"/> </authorization> </system.web> </location> <location path="2"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location> <location path="3"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location> <location path="4"> <system.web> <authorization> <allow users="member" /> <deny users="?" /> </authorization> </system.web> </location> <location path="5"> <system.web> <authorization> <allow users="?" /> </authorization> </system.web> </location> <location path="6"> <system.web> <authorization> <allow users="?"/> </authorization> </system.web> </location>
sibweo0916 2014-09-04
  • 打赏
  • 举报
回复
引用 5 楼 ayanamireizero 的回复:
[quote=引用 3 楼 sibweo0916 的回复:] [quote=引用 2 楼 ayanamireizero 的回复:]
  <location path="不需要登陆就可访问的页面">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
有多个页面不需要验证的怎么设[/quote] web.config里多加N个以上配置[/quote] 现在的问题是 像这种 <location path="不需验证页面"> <system.web> <authentication mode="Forms"> <forms name=".ASPXAUTH" loginUrl="~/home/login" protection="All" path="/" timeout="20"/> </authentication> <authorization> <deny users="?"></deny> </authorization> </system.web> </location> 是可以的,但是 <location path="不需验证页面1"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location> <location path="不需验证页面2"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location> <authentication mode="Forms"> <forms name=".ASPXAUTH" loginUrl="~/home/login" protection="All" path="/" timeout="20"/> </authentication> <authorization> <deny users="?"></deny> </authorization> 是不行的 像 <location path="不需验证页面1"> <system.web> <authentication mode="Forms"> <forms name=".ASPXAUTH" loginUrl="~/home/login" protection="All" path="/" timeout="20"/> </authentication> <authorization> <deny users="?"></deny> </authorization> </system.web> </location> <location path="不需验证页面2"> <system.web> <authentication mode="Forms"> <forms name=".ASPXAUTH" loginUrl="~/home/login" protection="All" path="/" timeout="20"/> </authentication> <authorization> <deny users="?"></deny> </authorization> </system.web> </location> 也是不行的?? 多个页面不需验证是怎么做的,有没有别的方法
ayanamireizero 2014-09-04
  • 打赏
  • 举报
回复
引用 3 楼 sibweo0916 的回复:
[quote=引用 2 楼 ayanamireizero 的回复:]
  <location path="不需要登陆就可访问的页面">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
有多个页面不需要验证的怎么设[/quote] web.config里多加N个以上配置
sibweo0916 2014-09-04
  • 打赏
  • 举报
回复
引用 1 楼 webdiyer 的回复:
视图怎么可能验证?这又不是webform,需要验证的应该是controller和action,使用AuthorizeAttribute: [Authorize] public ActionResult NeedAuthorize(){...} 也可以在整个controller之上使用: [Authorize] public class MyController:Controller{....} 不需要验证的action可以使用 AllowAnonymousAttribute: [AllowAnonymous] public ActionResult Login(string returnUrl){...} MVC中早不用你说的那种在web.config中设置的验证方法了
引用的公司级同一登录接口,现在只能在web.config里面设置
sibweo0916 2014-09-04
  • 打赏
  • 举报
回复
引用 2 楼 ayanamireizero 的回复:
  <location path="不需要登陆就可访问的页面">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
有多个页面不需要验证的怎么设
ayanamireizero 2014-09-04
  • 打赏
  • 举报
回复
  <location path="不需要登陆就可访问的页面">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
webdiyer 2014-09-04
  • 打赏
  • 举报
回复
视图怎么可能验证?这又不是webform,需要验证的应该是controller和action,使用AuthorizeAttribute: [Authorize] public ActionResult NeedAuthorize(){...} 也可以在整个controller之上使用: [Authorize] public class MyController:Controller{....} 不需要验证的action可以使用 AllowAnonymousAttribute: [AllowAnonymous] public ActionResult Login(string returnUrl){...} MVC中早不用你说的那种在web.config中设置的验证方法了

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧