簡體   English   中英

使用 asp.net Identity 進行 Azure AD 身份驗證以進行授權

[英]Azure AD authentication with asp.net Identity for authorisation

我試圖在整個互聯網上尋找,但看不到我如何實現我的要求。 這是我的企業應用程序,它使用 Asp.net Identity 進行基於表單的身份驗證。 我擴展了 User 和 Role 以及 Groups 以在我的代碼中提供授權。 (注意:不使用任何組/角色指令)。

現在我被要求研究更改代碼以適應 Azure Active Directory 身份驗證的可能性。 我嘗試閱讀如何注冊應用程序、將用戶發送到 Azure 站點進行身份驗證、取回令牌等。但是我被困在“之后會怎樣?” 我已經驗證用戶如何使用我現有的 Asp.net Identity 模型,其中用戶存儲在 sql 數據庫中。 如何使用此令牌關聯現有用戶。

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

我什至嘗試手動將兩個包並排保存,我必須指出用戶被發送到 Azure 上的身份驗證,轉移回主頁並再次以永無止境的循環登錄 Azure AD。

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

編輯:我嘗試創建單獨的 Web 服務,它將對用戶進行身份驗證並發送 JWT 令牌。 如果我直接在瀏覽器上調用它,它會起作用,但是,當我嘗試從我的網絡應用程序調用此服務時,我收到奇怪的錯誤

 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);
......

試試這個:

var client = new RestClient("https://login.microsoftonline.com/{tenant- 
         Id}/oauth2/v2.0/token");
var request = new RestRequest(Method.POST);         
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");         
request.AddHeader("grant_type", "password");
request.AddParameter("application/x-www-form-urlencoded", 
        "grant_type=password&client_id={client-Id}&client_secret={client- 
        secret}&scope={scopeurl}&userName={username}&password={password}", 
        ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var json = response.Content;
var JSONObject = JObject.Parse(json);
var token = (string)JSONObject["access_token"];

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

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

您需要在此處注冊您的應用程序。 注冊應用程序時將回調 uri 設置為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 包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM