2

I'm trying to make an IdentityServer4 starting from a Microsoft example.

The example contains an Api project and an mcv client to authenticate and call the api.

enter image description here

Api project Startup.cs:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        services.AddAuthentication("Bearer")
            .AddJwtBearer("Bearer", options =>
            {
                options.Authority = "http://localhost:5000";
                options.RequireHttpsMetadata = false;

                options.Audience = "api1";
            });
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

Api IdentityController.cs:

namespace Api.Controllers
{
    [Route("identity")]
    [Authorize]
    public class IdentityController : ControllerBase
    {
        public IActionResult Get()
        {
            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }
    }
}

this code in MvcClient invoke correctly the action of IdentityController:

public async Task<IActionResult> CallApi()
{
        var accessToken = await HttpContext.GetTokenAsync("access_token");

        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        var content = await client.GetStringAsync("http://localhost:5001/identity");

        ViewBag.Json = JArray.Parse(content).ToString();
        return View("json");
}

I added one action named Test to IdentityController:

namespace Api.Controllers
{
    [Route("identity")]
    [Authorize]
    public class IdentityController : ControllerBase
    {
        public IActionResult Get()
        {
            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }

        public IActionResult Test()
        {
            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }
    }
}

But this code throw 404 not found in McvClient:

public async Task<IActionResult> CallTestApi()
{
        var accessToken = await HttpContext.GetTokenAsync("access_token");

        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        var content = await client.GetStringAsync("http://localhost:5001/identity/Test");

        ViewBag.Json = JArray.Parse(content).ToString();
        return View("json");
}

enter image description here

2
  • 1
    404, I think your server is not started or endpoint does not exist Commented May 27, 2020 at 12:41
  • the server is starded, localhost:5001/identity works fine. but how to call another action? Commented May 27, 2020 at 12:56

1 Answer 1

1

This sample uses HTTP verb route template. As you see [Route("identity")] is on controller level, and Get falls under default get for the controller. But for Test action you need to add [HttpGet("test")]. Read more about routing here. The code would be like this:

namespace Api.Controllers
{
    [Route("identity")]
    [Authorize]
    public class IdentityController : ControllerBase
    {
        public IActionResult Get()
        {
            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }

        [HttpGet("test")]
        public IActionResult Test()
        {
            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.