C# 授权在ASP.NET MVC 5中不起作用

C# 授权在ASP.NET MVC 5中不起作用,c#,asp.net-mvc-5,authorization,owin,C#,Asp.net Mvc 5,Authorization,Owin,免责声明:这是我第一次使用ASP.NETMVC5 我不知道为什么它不起作用。我无法让我的MVC5应用程序授权用户。我在以前的版本(2、3和4)中已经做到了这一点,但在OWIN中似乎无法实现 我正在使用启用了所需功能的本地IIS: 编辑: 我在IIS上使用SSL,在C上使用RequireHttps# 代码如下: protected void Application_Start() { GlobalFilters.Filters.Add(new AuthorizeAttribute());

免责声明:这是我第一次使用ASP.NETMVC5

我不知道为什么它不起作用。我无法让我的MVC5应用程序授权用户。我在以前的版本(2、3和4)中已经做到了这一点,但在OWIN中似乎无法实现

我正在使用启用了所需功能的本地IIS:

编辑

我在IIS上使用SSL,在C上使用RequireHttps#

代码如下:

protected void Application_Start()
{
    GlobalFilters.Filters.Add(new AuthorizeAttribute());
}
Startup.Auth.cs

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/admin/account/login")
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseGoogleAuthentication();
即使我正在使用Global Authorize,我也试图“强制”它,以确定这是否是问题所在:

public class HomeController : Controller
{
    [Authorize]
    public ActionResult Index()
    {
        return View();
    }
}
没有运气。。。 我不确定OWIN是否需要这样做,但我甚至尝试启用表单身份验证:

<authentication mode="Forms" />


有什么想法吗?

既然你的应用程序是用IIS运行的,试着将nuget软件包添加到你的应用程序中。

试试这篇文章,它对我很有帮助 . 我不确定您是否使用了声明,因为您在问题中没有显示它,这对于创建身份验证票证进行身份验证至关重要

另外,在使用SSL时,如果仍然不起作用,请注意CookieSecure属性


我希望这将有助于

您是否得到任何错误,或者更准确地说,您得到的输出是什么?@StephenReindl没有错误,我可以访问我不应该访问的页面(Authorize属性)。我只是在主页/索引中看到页面。。。有趣的。。。为什么在应用程序启动时添加authorized属性?我的启动代码在行FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)中执行以下操作;RouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles);其余部分在Startup.cs…@StephenReindl中完成,因为我希望我的整个应用程序处于锁定模式。只有少数操作可以匿名访问。我明白了。是否可以检查启动代码是否已执行(添加日志/断点)?我在启动代码未执行的情况下看到过这种行为……谢谢,但不是这样。我很久以前就读过,尽管我已经安装了这个软件包,但我还是没有更新它。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/admin/account/login")
    CookieSecure = CookieSecureOption.Never
});