How to write an integration test for Identity Server 4 in asp.net mvc core

I have a web api using an Identity 4 server. I don’t know where to start writing an integration test. I have a login controller with a username and password that is used for the ResourceOwnerPassword Grant type. Below is my code.

controller.

[Route("Authentication/Login")]
public async Task<IActionResult> WebApiLogin(string username, string password)
{
    var accessToken = await UserAccessToken.GenerateToken(username, password);
    return new JsonResult(accessToken);
}

Class for creating a token

public async Task<string> GenerateToken(string username, string password)
{
    //discover endpoint for metadata
    var disco = await DiscoveryClient.GetAsync("http://localhost:5000");

    //request token
    var clientToken = new TokenClient(disco.TokenEndpoint, "client", "secret");
    //var tokenResponse = await clientToken.RequestClientCredentialsAsync("Payment");
    var tokenResponse = await clientToken.RequestResourceOwnerPasswordAsync(username, password, "IntegrapayAPI");

    if (tokenResponse.IsError)
    {
        //Error tokenResponse.Error
        return tokenResponse.Error;
    }
    return tokenResponse.Json.ToString();
}

IdentityServer Project startup class.

public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentityServer()
        .AddTemporarySigningCredential()
        .AddInMemoryApiResources(Config.GetApiResources())
        .AddInMemoryClients(Config.GetClients());
    //.AddTestUsers(Config.GetUsers());

    services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();

    // Add framework services.
    //services.AddMvc();
}
+4
source share
1 answer

: fooobar.com/questions/558750/... : , . GitHub repo, API, MVC, , IdentityServer .

, API IdentityServer. IdentityServer , API .

samples, .

+4

Source: https://habr.com/ru/post/1667713/


All Articles