AllowAnonymous属性不工作MVC 5

Gre*_*aue 6 asp.net-mvc authorization azure asp.net-mvc-5

在Azure门户内部,我将App Service Authentication设置为"On"对于我的Web App,我使用AAD作为身份验证提供程序.

到目前为止,这已经很好用,我需要一个允许匿名用户的端点,但是该属性[AllowAnonymous]不起作用,我仍然需要登录.

码:

[Authorize]
[RoutePrefix("users")]
public class UsersController : Controller
{
    [Route("register/{skypeid}")]
    public ActionResult Register(string skypeid)
    {
            ///stuff...            
        }
        catch (Exception ex)
        {
            return Content(ex + "");
        }

        ViewBag.Name = name;
        return View();

    }

    [AllowAnonymous]
    [Route("exists/{skypeid}")]
    public ActionResult Exists(string skypeid)
    {
        return Content("Hello " + skypeid);
    }
Run Code Online (Sandbox Code Playgroud)

我认为代码是正确的,它是否与我的Web App使用App Service Authentication这一事实有关?

编辑:所以,我发现问题的根源,在Azure中,如果您将"未经过身份验证时采取的操作"设置为"使用Azure Active Directory登录",它绝不会允许匿名.

但是,如果我将其更改为允许匿名,则在尝试使用[Authorize] -Attribute访问控件时不会提示用户登录,它只是告诉我"您无权查看此目录或页面".这是有意的吗?这看起来很奇怪.如果有[Authorize] -Attribute,我希望用户被重定向到Login.

屏幕截图清晰:

在此输入图像描述 在此输入图像描述

ale*_*xey 10

如果有,请检查您的web.config

<authorization>
  <deny users="?" />
</authorization>
Run Code Online (Sandbox Code Playgroud)

它的覆盖[AllowAnonymous]添加

<location path="YourController/AnonymousMethod">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
Run Code Online (Sandbox Code Playgroud)

允许匿名访问


Adr*_*all 1

我刚刚在我的书中写到了这一点 - http://aka.ms/zumobook - 请参阅第 6 章中的 MVC 部分。

其基本要点是您需要做更多的事情来启用身份验证;最具体地说,您需要设置一个身份验证管道(Azure Mobile Apps Server SDK 将为您执行此操作),并且需要在 Web.config 中设置表单重定向:

<system.web>
  <compilation debug="true" targetFramework="4.5.2"/>
  <httpRuntime targetFramework="4.5.2"/>
  <authentication mode="Forms">
    <forms loginUrl="/.auth/login/aad" timeout="2880"/>
  </authentication>
</system.web>
Run Code Online (Sandbox Code Playgroud)

由于将移动应用 SDK 添加到 ASP.NET 应用程序有多个详细信息,因此我将参考引用的章节来了解这些详细信息。