Previously developed was an application that had authentication with Microsoft identity providers via OpenIdConnect:
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
options.Instance = "/";
options.TenantId = TenantId;
options.ClientId = builder.Configuration.GetValue<string>("AzureADClientID");
options.ClientSecret = builder.Configuration.GetValue<string>("AzureADSecret");
options.CallbackPath = "/signin-oidc";
options.SaveTokens = true; // Ensure tokens are saved
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseMiddleware<CustomAuthenticationMiddleware>();
Now, I am wanting to add a custom layer of middleware to this project. It should use HttPContext, extracting it's information to perform the custom MiddleWare logic. I have this custom middleware added to be included in my Program.cs
file. Here is where the custom middleware begins execution:
[Authorize]
public async Task Invoke(HttpContext context)
{
bool authValid = false;
authValid = await CheckIfUserAPIAuth(context);
// bool authValid = true;
if (authValid)
{
await _next.Invoke(context);
}
else
{
context.Response.StatusCode = 401; //Unauthorized
return;
}
}
However, the context.User
field is set to IsAuthenticated = false
when this is invoked. Ideally, I would like the User information to be their system information, but I can also ultimately use the AzureAD information, such as AD-GUID, to continue with my custom logic. However, the current context looks like this:
Previously developed was an application that had authentication with Microsoft identity providers via OpenIdConnect:
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
options.Instance = "https://login.microsoftonline.com/";
options.TenantId = TenantId;
options.ClientId = builder.Configuration.GetValue<string>("AzureADClientID");
options.ClientSecret = builder.Configuration.GetValue<string>("AzureADSecret");
options.CallbackPath = "/signin-oidc";
options.SaveTokens = true; // Ensure tokens are saved
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseMiddleware<CustomAuthenticationMiddleware>();
Now, I am wanting to add a custom layer of middleware to this project. It should use HttPContext, extracting it's information to perform the custom MiddleWare logic. I have this custom middleware added to be included in my Program.cs
file. Here is where the custom middleware begins execution:
[Authorize]
public async Task Invoke(HttpContext context)
{
bool authValid = false;
authValid = await CheckIfUserAPIAuth(context);
// bool authValid = true;
if (authValid)
{
await _next.Invoke(context);
}
else
{
context.Response.StatusCode = 401; //Unauthorized
return;
}
}
However, the context.User
field is set to IsAuthenticated = false
when this is invoked. Ideally, I would like the User information to be their system information, but I can also ultimately use the AzureAD information, such as AD-GUID, to continue with my custom logic. However, the current context looks like this:
If you try to use SignIn
explicitly I think it should work, basically you're saying if the incoming token validated successfully then set the user into HttpContext.
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
options.Instance = "https://login.microsoftonline.com/";
options.TenantId = TenantId;
options.ClientId = builder.Configuration.GetValue<string>("AzureADClientID");
options.ClientSecret = builder.Configuration.GetValue<string>("AzureADSecret");
options.CallbackPath = "/signin-oidc";
options.SaveTokens = true; // Ensure tokens are saved
options.Events.OnTokenValidated = async context =>
{
await context.HttpContext.SignInAsync(
OpenIdConnectDefaults.AuthenticationScheme, context.Principal);
};
options.Events.OnAuthenticationFailed = context =>
{
// check the context.Exception or response
return Task.CompletedTask;
};
});