2017-06-13 60 views
0

我正在將IdentityServer3添加到現有網站(NopCommerce是特定的)。它擁有自己的註冊和認證系統,但我們還需要提供OpenID Connect,以便集成後端應用程序。對後端的調用需要爲當前用戶提供一個id_token,後端進行驗證以確認身份。以編程方式創建與IdentityServer3的OpenID Connect id_token

我發現了有關如何使用現有成員資格數據庫爲IdentityServer3提供用戶數據進行檢查的信息,但是我對如何爲每個用戶生成id_token存在困惑。我想最明顯的答案是用IdentityServer替換網站登錄,但是這會給項目的其他部分帶來進一步的問題。理想情況下,我希望用戶以正常方式登錄,然後在IdentityServer中調用一個方法來生成id_token。

這可能嗎?我一直在四處打獵,但到目前爲止還找不到任何東西。我發現的最好的答案是programmatically sign in to identityserver3。我認爲這是建議向IdentityServer發送一個HTTP帖子,但它感覺有點不好意思。

我也發現Implementing OAuth 2.0 and OpenId Connect provider using IdentityServer3 with existing login server and membership provider,但我不得不承認它假設了很多我還沒有的知識(還)。

+0

我已經試過代碼[編程登錄identityserver3(https://開頭stackoverflow.com/questions/40949893/programmatically-sign-in-to-identityserver3)並意識到這總是返回一個access_token,不幸的是,後端必須有id_token。 – Suddy

+0

看到這個類:https://github.com/IdentityServer/IdentityServer3/blob/master/source/Core/Services/Default/DefaultTokenService.cs - 如果你可以安裝它,你可以用它生成一個id_token – stombeur

+0

感謝找到我認爲關鍵部分雖然在「_if_我可以實例化它」的課程。到目前爲止,我已經實現了JohnC在他的回答中所建議的一個簡單的IdentityServer,並且正在探索如何滿足我們的要求。 – Suddy

回答

0

我的解決方案使用NopCommece外部認證插件工作,但我並不滿意不得不離開NOP認證和註冊時IdentityServer使用NOP數據庫了。通過外部認證路線似乎是獲得OpenID Connect id_token的唯一途徑。休息一下,一段時間來,雖然重溫代碼後,我發現了以下內容:

https://identityserver.github.io/Documentation/docsv2/configuration/serviceFactory.html https://identityserver.github.io/Documentation/docsv2/configuration/serviceFactory.html

通過實現定製服務,IdentityServer允許您使用令牌創建和生成,它使用依賴注入系統混亂使您可以訪問默認服務的安裝版本。

我以前曾遵循一個答案,它向令牌端點發送了一個用戶名和密碼。 OpenID規範說這應該只返回access_token,這正是IdenttyService中的DefaultTokenService所做的。然而,通過添加一個CustomTokenResponseGenerator,我能夠重新使用該請求來創建並返回一個id_token。

CustomTokenResponse類:

internal class CustomTokenResponseGenerator : ICustomTokenResponseGenerator 
{ 

    protected ITokenService _tokenService; 

    public CustomTokenResponseGenerator(ITokenService tokenService) 
    { 
     _tokenService = tokenService; 
    } 

    public Task<TokenResponse> GenerateAsync(ValidatedTokenRequest request, TokenResponse response) 
    { 
     var tokenRequest = new TokenCreationRequest 
     { 
      Subject = request.Subject, 
      Client = request.Client, 
      Scopes = request.ValidatedScopes.GrantedScopes, 
      //Nonce = request.AuthorizationCode.Nonce, 

      ValidatedRequest = request 
     }; 
     var idToken = _tokenService.CreateIdentityTokenAsync(tokenRequest); 
     idToken.Wait(); 
     var jwt = _tokenService.CreateSecurityTokenAsync(idToken.Result); 
     response.IdentityToken = jwt.Result; 
     return Task.FromResult(response); 
    } 
} 

如何注入定製服務Startup.cs:

factory.TokenService = new Registration<ITokenService, TokenService>(); 
factory.CustomTokenResponseGenerator = new Registration<ICustomTokenResponseGenerator, CustomTokenResponseGenerator>(); 
+0

它看起來像你的代碼中,你正在調用一個異步方法,而不使用前面的await關鍵字。如果該方法確實是一種異步方法,則需要在方法GenerateAsync之前添加async關鍵字,然後在調用CreateIdentityTokenAsync和CreateSecurityTokenAsync時使用await關鍵字。然後只需返回響應,而不需要FromResult。 –

0

當用戶登錄到NopCommerce應用程序時,您可以向身份服務器發送HTTP授權請求。確保將授權請求發送給使用prompt = none的idsrv,這樣,如果用戶已經登錄,您將獲得id_token或access_token,而不會再次向用戶顯示同意。

function getAuthorizeRequest() { 
var url = global.appSettings.identityServerURL 
        + "/connect/authorize?client_id=siteB&response_type=id_token token&redirect_uri=" 
        + global.appSettings.siteBUrl + "/Main/NopCommerceapp&scope=siteBscope openid email roles&prompt=none&nonce="76767xz676xzc76xz7c67x6c76"      
return encodeURI(url);} 

結帳idsrv授權端點https://identityserver.github.io/Documentation/docsv2/endpoints/authorization.html

+0

謝謝,但我不太確定我是否遵守。文檔中提到「在請求期間不會顯示任何用戶界面,如果不可能(例如,因爲用戶必須登錄或同意)纔會返回錯誤」。我的用戶已經登錄到NopCommerce,但沒有登錄到IdentityServer,所以他們只是得到一個錯誤響應。 – Suddy

0

我覺得你最好的辦法是實行IdentityServer3並把它從你現有的NOP的會員數據庫中讀取數據。然後創建一個Web API應用程序,它可以使用您的IdentityServer3設置自行運行。在Web API內部,您可以實現後端應用程序所需的所有功能,讀取和寫入Nop數據庫。

讓您的Nop前端用戶界面與您的後端API分開。如果你按照下面的兩個鏈接,你應該能夠快速啓動並運行。根據約翰·C·的答案

Creating the simplest OAuth2 Authorization Server, Client and API
MVC Authentication & Web APIs

+0

謝謝。我採用了簡單的服務器方法和客戶用戶服務(https://identityserver.github.io/Documentation/docsv2/advanced/userService.html),該服務讀取表單我的Nop客戶數據庫。 Nop能夠將其用作外部登錄提供程序(http://docs.nopcommerce.com/display/en/External+Authentication+Method)。 – Suddy