简体   繁体   English

仅当未在asp.net MVC中进行身份验证时,才将用户重定向到登录页面

[英]Redirect users to logon page only if not authonticated in asp.net MVC

NET MVC project i have following tag in in web.config file NET MVC项目,我在web.config文件中有以下标记

<authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="2880"/> </authentication>

This causes even the authenticated users but unauthorized resource requested users to redirect to logon page. 这甚至会导致经过身份验证的用户但未经授权的资源请求用户重定向到登录页面。 but i need only to redirect this page if user try to access unauthorized page and not already authenticated(logged on) and redirect to custom page. 但如果用户尝试访问未经授权的页面并且尚未通过身份验证(登录)并重定向到自定义页面,则仅需要重定向此页面。

Is there easy way to do this without writing custom action filter? 没有编写自定义动作过滤器的简便方法吗?

All that this line does in web.config is to simply define the timeout of the authentication cookie and the login url. 该行在web.config所做的全部工作只是简单地定义身份验证cookie和登录URL的超时。 It is your code that decides which parts of the site are authenticated or no, by for example decorating your controllers and/or actions with the [Authorize] attribute. 是您的代码(例如通过使用[Authorize]属性装饰您的控制器和/或操作)来决定对站点的哪些部分进行身份验证。

please check your "authorization" setting in web.config file. 请检查web.config文件中的“授权”设置。 It should be somewhat 应该有点

<authorization>
      <deny users="?" />
      <allow users="*" />
    </authorization>

also when user authenticates successfully make sure you call 此外,当用户成功通过身份验证时,请确保您致电

FormsAuthentication.SetAuthCookie(<username>, false); 

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

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