繁体   English   中英

AllowAnonymous属性不工作MVC 5

[英]AllowAnonymous Attribute not working 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);
    }

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

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

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

屏幕截图清晰:

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

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

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

它的覆盖[AllowAnonymous]添加

<location path="YourController/AnonymousMethod">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>

允许匿名访问

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

它的基本要点是你需要做更多的事情来启用身份验证; 最具体地说,您需要设置一个auth管道(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>

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

暂无
暂无

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

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