授权不适用于ASP.NET MVC 5

时间:2015-02-02 21:07:14

标签: c# asp.net-mvc-5 authorization owin

  

免责声明:这是我第一次使用ASP.NET MVC 5

我不知道它为什么不起作用。我无法让我的MVC5应用程序授权用户。我在以前的版本(2,3和4)中完成了这个,但我似乎无法在OWIN中使用它。

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

IIS Features

  

编辑

     

我在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();

即使我正在使用全局授权,我试图“强制”它以查看这是否是问题:

public class HomeController : Controller
{
    [Authorize]
    public ActionResult Index()
    {
        return View();
    }
}

没有运气...... 我不确定OWIN是否有必要,但我甚至尝试过启用表单身份验证:

<authentication mode="Forms" />
  

编辑[2]

     

好吧,我发现了问题...... IIS!最后!会的,会的   有谁知道如何解决这个问题?我需要什么特别的东西才能运行OWIN   IIS?我现在可以工作,但很快我将不得不部署应用程序   可能在服务器中遇到同样的问题......

我已经读过这些:

How do you login/authenticate a user with Asp.Net MVC5 RTM bits using AspNet.Identity?

Authorize attribute not working MVC 5

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

由于您的应用运行IIS,请尝试将Microsoft.Owin.Host.SystemWeb nuget包添加到您的应用中。

答案 1 :(得分:2)

试试这篇文章,对我有帮助 OWIN setup。 我不确定您是否使用了在您的问题中没有显示的声明,这对于创建身份验证身份验证票件至关重要

此外,当您使用SSL时,如果仍然无效,请注意CookieSecure属性CookieSecureOption

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/admin/account/login")
    CookieSecure = CookieSecureOption.Never
});

我希望它会有所帮助