2017-01-03 50 views
3

在Azure门户内部我设置应用程序服务身份验证“On”对于我的Web应用程序,我使用AAD作为身份验证提供程序。AllowAnonymous属性不工作MVC 5

这直到现在伟大的工作,我需要一个端点,将允许匿名用户,但是属性[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身份验证的事实有关?

编辑: 于是,我找到了问题的根源,在Azure中,如果你设置为“未通过身份验证时采取的措施”,以“拍在与Azure中的Active Directory”,但这绝不允许匿名。

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

截图的清晰度:

enter image description here enter image description here

+0

为您的应用程序启用匿名身份验证? https://www.iis.net/configreference/system.webserver/security/authentication/anonymousauthentication –

+0

@ChrisPratt我无法在IIS管理器中看到我的网站,一直这样。无论哪种方式,它都默认启用,除非您可以在Azure Portal中禁用它,否则应该启用它。 –

回答

1

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

它的基本要点是,你需要做更多一点来启用身份验证;最具体,你需要建立一个权威性的管道(Azure的移动应用程序服务器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应用程序中,我会参考参考章节了解这些详细信息。

+0

谢谢,我接受了这个答案,我决定改变授权方法,并使用Open ID Connect,让我有更多的控制权。 –

-1

因为Controller权限设置[Authorize]所以当请求尝试打Action他发现,他需要的访问权限controller,从控制器删除Authorize并添加上述各行动。

+0

我也试过,结果也一样。谢谢你,虽然 –

+0

用控制器上面的[AllowAnonymous]取代它,它将起作用 –

+0

这也不起作用,结果相同。 –

1

检查你的web.config,如果你有

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

其覆盖[使用AllowAnonymous] 添加

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

允许匿名访问