I am developing a simple web app in asp.net core 2.0 to test identity server 4's implementation. I have created a new project (WebApplication (Model-View-Controller). I can successfully generate accessToken and refreshToken using TokenClient.RequestRefreshTokenAsync method but when I am trying to call any action that has Authorize Attribute on it, it gives me login page as html in postman response's section. I have also passed the accessToken in Authorization header of the call. I am doing it for the first time so I think I have some issue in startup file. Here is the code:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();
// configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<ApplicationUser>();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
}
And here is the action that is creating a token:
var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return BadRequest();
}
var tokenClient = new TokenClient(disco.TokenEndpoint, "ro.client", "secret");
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync(model.Email, model.Password, "api1");
and here is the action with Authorize Attribute:
[HttpGet]
[Authorize]
public ObjectResult Test()
{
return new ObjectResult(Ok());
}
and here is the postman call: Postman Call
Can anybody tell what am I doing wrong. Thanks