简体   繁体   English

如何在MVC项目的Web.Config文件中的身份验证标签内正确编写表单标签

[英]How to correctly write forms tag inside authentication tag in Web.Config file in mvc project

I have a home controller, Employee controller in mvc. 我有一个家庭控制器,mvc中的Employee控制器。

Home controller index page is login page. 家庭控制器索引页面是登录页面。

Now when user tries to access Employee controller it should automatically redirect to login page that is home controller index. 现在,当用户尝试访问Employee控制器时,它应该自动重定向到作为家庭控制器索引的登录页面。

I am doing it using web.config file. 我正在使用web.config文件。 All working but it not redirecting to login page instead showing access denied error. 一切正常,但未重定向到登录页面,而是显示访问被拒绝错误。

Here is the code: 这是代码:

<system.web>
<authentication mode="Forms">
  <forms loginUrl="Home/Index" />
</authentication>
<authorization>
  <deny users="*"/>
</authorization>

<location path="Home">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>

In MVC, you should be using the Authorize attribute rather than changing your web.config . 在MVC中,您应该使用Authorize属性,而不是更改web.config For example: 例如:

[Authorize]
public class EmployeeController : Controller
{
    //snip
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM