2017-01-03 52 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> 

允許匿名訪問