I am trying to upgrade a project from .Net core 1.1 to .Net core 2.0 there are a lot of breaking changes. One of the things i am currently having an issue with is that HttpContext.Authentication
is now obsolete.
I have been trying to figure out how to get the Access token for the current request. I need to make a call to another API which requires a bearer token.
Old Method .Net core 1.1
[Authorize]
public async Task<IActionResult> ClientUpdate(ClientModel client)
{
var accessToken = await HttpContext.Authentication.GetTokenAsync("access_token");
return View();
}
Method .Net core 2.0
This is not working becouse context isnt registered.
[Authorize]
public async Task<IActionResult> ClientUpdate(ClientModel client)
{
var accessToken = await context.HttpContext.GetTokenAsync("access_token");
return View();
}
Unable to resolve service for type 'Microsoft.AspNetCore.Http.HttpContext'
I tried registering it but that doesnt work either
public ConsoleController(IOptions<ServiceSettings> serviceSettings, HttpContext context)
In startup.cs
services.TryAddSingleton<HttpContext, HttpContext>();
Update:
This returns null
var accessToken = await HttpContext.GetTokenAsync("access_token");
Startup.cs ConfigureServices
I wouldn't be surprised if it was something in the startup as there were a lot of breaking changes here as well.
services.Configure<ServiceSettings>(Configuration.GetSection("ServiceSettings"));
//services.TryAddSingleton<HttpContext, HttpContext>();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMvc();
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.Authority = "http://localhost:5000";
options.ClientId = "testclient";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.RequireHttpsMetadata = false;
options.GetClaimsFromUserInfoEndpoint = true;
});
Startup.cs Configure
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Startup.cs
Controller.cs constructor
This should work
You need to specify the external schema to retrieve the token.
Real thanks, this is perfect !
I had this work, but with our azure tenant dedicated authority. Simply replace ****** with your tenant name.
You also can use tenant id. Simply insert your tenant id after https://login.microsoftonline.com/
A little change from Azharuddin answer
Register the service instance in Startup method like
And inject the dependency in your controller like
And retrieve the access token in your action like
It ended up being a configuration issue. There needs to be a link between AddAuthentication and AddOpenIdConnect in order for it to read the cookie into the headers.
Controller
Access token is now populated.
Note: I ended up digging it out of this project Startup.cs
.Net core 2.1 to access JWT bearer token