2016-08-24 407 views
2

我試圖通過網絡查找所有內容,但無法看到我如何實現所要求的內容。這是我的企業應用程序,它使用Asp.net身份進行基於表單的身份驗證。我已將用戶和角色與組一起擴展以在我的代碼中提供授權。 (注意:不使用任何組/角色指令)。用asp.net進行Azure AD身份驗證身份驗證

現在我被要求考慮更改代碼以適應Azure Active Directory身份驗證的可能性。我嘗試閱讀如何註冊應用程序,將用戶發送到Azure站點進行身份驗證,取回令牌等。但是,我被困在'事後?'中。我已驗證用戶如何使用我的現有Asp.net身份模型,其中用戶存儲在SQL數據庫中。如何使用此令牌關聯現有用戶。

此外,當我將項目更改爲允許Azure AD時,它將刪除Aspnet.Identity包,因爲它與Azure AD不兼容!

我甚至嘗試手動同時保存兩個包,我得到指向用戶發送到Azure上進行身份驗證的位置,轉移回主頁並再次登錄Azure AD的永無止境的循環。

總結這個問題,我如何從AAD認證用戶並繼續使用現有的角色和組授權。

編輯︰ 我試着創建單獨的Web服務,將驗證用戶和發送JWT令牌。它的工作原理,發現如果我直接調用它的瀏覽器,但是當我試圖從我的web應用程序調用這個服務,我得到奇怪的錯誤

Application with identifier 'a2d2---------------' was not found in the directory azurewebsites.net 
這裏

怪異的一部分是目錄的名稱是「azurewebsites.net」,而不是我正在使用的默認目錄。

更新 這裏是代碼拋出錯誤

public async Task<ActionResult> Login(string returnUrl) 
     { 


      try 
      { 

       // get the access token 
       AuthenticationContext authContext = new AuthenticationContext(authority, new TokenCache()); 

       var clientCredential = new ClientCredential(clientId, password); 
//Error on below line 
       AuthenticationResult result = await authContext.AcquireTokenAsync(resourceId, clientCredential); 


       // give it to the server to get a JWT 
       HttpClient httpClient = new HttpClient(); 
       httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); 
...... 

回答

0

我也有類似的問題,所以我創建了一個Office 365的owin安全插件。我在github上分享了代碼。它基於codeplex上的katana project

你可以在https://github.com/chadwjames/wakizashi找到源代碼。

您需要註冊您的應用程序here。在註冊應用程序時,將回撥設置爲https://yourdomain/signin-office365

應用程序ID是您的客戶端ID,密碼是您的客戶端密碼。

一旦你註冊了它,你可以修改Startup.Auth.cs並在ConfigureAuth方法中添加這樣的內容。

 //setup office 365 
     var office365Options = new Office365AuthenticationOptions 
     { 
      ClientId = ConfigurationManager.AppSettings["ada:ClientId"], 
      ClientSecret = ConfigurationManager.AppSettings["ada:ClientSecret"], 
      Provider = new Office365AuthenticationProvider 
      { 
       OnAuthenticated = async context => 
       { 
        await 
         Task.Run(
          () => context.Identity.AddClaim(new Claim("Office365AccessToken", context.AccessToken))); 
       } 
      }, 
      SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie 
     }; 

     office365Options.Scope.Add("offline_access"); 
     app.UseOffice365Authentication(office365Options); 

當我有更多的時間,我希望爲此創建一個nuget包。