如何将所有未经授权的请求重定向到/ Account / Login?ReturnUrl =%2f? MVC

时间:2016-04-29 05:13:40

标签: asp.net asp.net-mvc

如何将所有未经授权的请求重定向到/Account/Login?ReturnUrl=%2f?

.NET framework version 4.5

使用ASP.NET Identity,所以我不喜欢

<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>

1 个答案:

答案 0 :(得分:1)

假设您正在使用配置为使用Owin的最后一个MVC 5默认模板,您可以通过在Startup类中配置它来实现您想要的功能:

[assembly: OwinStartupAttribute(typeof(MvcApplication.Startup))]
namespace MvcApplication
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login")
                // Configure the rest as you need
            });
        }
    }
}

有关更多信息,您可以阅读由MVP撰写的这篇精彩的article

相关问题