Asp.net mvc 注册页面重定向到登录页面,即使具有AllowAnonymous属性

Asp.net mvc 注册页面重定向到登录页面,即使具有AllowAnonymous属性,asp.net-mvc,asp.net-mvc-4,login,forms-authentication,Asp.net Mvc,Asp.net Mvc 4,Login,Forms Authentication,我使用表单身份验证让用户登录到我的网站。但是,即使注册操作添加了[AllowAnonymous]属性,用户也无法在不登录的情况下访问注册页面 我的AccountController操作是: [HttpGet] [AllowAnonymous] public ActionResult Register() { return View("Register"); } [HttpPost] [ValidateAntiForgeryToke

我使用表单身份验证让用户登录到我的网站。但是,即使注册操作添加了[AllowAnonymous]属性,用户也无法在不登录的情况下访问注册页面

我的AccountController操作是:

[HttpGet]
    [AllowAnonymous]
    public ActionResult Register()
    {
        return View("Register");
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    [AllowAnonymous]
    public ActionResult Register(RegistrationModel registrationModel)
    {
        if (new UserAccounts().DoesUserExist(registrationModel.UserName, registrationModel.Email))
            ModelState.AddModelError(String.Empty, "User with same email or Username Already Exists");
        else
        {
            new UserAccounts().CreateUser(registrationModel);
            TempData["Success"] = "User has been created!";
        }

        return View();
    }
仅向经过身份验证的用户显示内容的新闻控制器操作为:

 [Authorize]
    public ActionResult News()
    {
        HomeNewsModel HomeNewsModel = null;

        try
        {
            ViewBag.IsNewsPage = true;
            NewsArticles NewsItems = new NewsArticles();
            ViewBag.Title = "Home";
            HomeNewsModel = new HomeNewsModel();
            HomeNewsModel.AllNews = NewsItems.GetAllNews();
            HomeNewsModel.NewsCategory = new NewsArticles().GetCategories();
        }
        catch (Exception)
        {

            throw;
        }
        return View(HomeNewsModel);
    }
这是我的Web.Config文件:

        <?xml version="1.0" encoding="utf-8"?>

     <configuration>
       <appSettings>
         <add key="webpages:Version" value="3.0.0.0"/>
         <add key="webpages:Enabled" value="false"/>
         <add key="ClientValidationEnabled" value="true"/>
         <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
       </appSettings>
       <connectionStrings>
         <add name="DBConnectionString" connectionString="server = localhost; uid = root; password= admin; persistsecurityinfo=True;database=sqlexpressdb;" />

       </connectionStrings>

       <system.web>
         <customErrors defaultRedirect="~/Error/" mode="Off" />
         <compilation debug="true" targetFramework="4.5.2"/>
         <httpRuntime targetFramework="4.5.2"/>
       <authentication mode="Forms">
         <forms loginUrl="~/Account/Login" />
       </authentication>
       </system.web>
       <location path="Account/Register">
         <system.web>
             <authorization>
             <allow users="?"/>
             </authorization>
         </system.web>
     </location>
       <runtime>

         <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
           <dependentAssembly>
             <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
             <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
           </dependentAssembly>
           <dependentAssembly>
             <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
             <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
           </dependentAssembly>
           <dependentAssembly>
             <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
             <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
           </dependentAssembly>
         </assemblyBinding>
       </runtime>
       <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>

我尝试过的事情:

  • 将[AllowAnonymous]属性置于控制器顶部
  • 在web.config中使用location path=“Register”/path=“~/Register”/path=“~/Account/Register…”
  • 从新闻控制器中删除[Authorize]属性会在我单击它时打开注册页面。 在新闻控制器操作中启用[Authorize]属性的情况下删除将在我在URL中键入注册页面时打开该页面

    但是,在启用了这两种组合的情况下,它会继续重定向到登录页面。

    很高兴它有所帮助:)

    只是在这里发布一个答案,以便访问该问题的人也可以获得类似问题的帮助


    在这种情况下,我们首先需要验证CSHTML页面的布局页面以及完整层次结构中的任何部分视图不得具有@Html.Action或@Html.RenderAction呈现具有Authorize属性的操作。

    是否在“\u布局”中呈现任何操作“仅授权用户可访问的页面?验证_布局页面中是否有Html.Action或Html.RenderAction,以及这些操作是否具有Authorize属性。很可能是这样,这正是问题所在。我在图像中嵌入了Html.Action而不是Html.ActionLink。新手犯了错误,因为我还是新手!非常感谢你!它成功了,我调用了一个@Html.Action,并将Authorize转换为Html注释。谢谢