I have a project that needs to be updated at .net core 3.1. The problem is that i don't know how to use this new feature from 3.1. I have my Identity Server Settings in appsettings.development.json, like this:
"IdentityServerSettings": {
"TokenUrl": "https://esample/token",
"ClientId": "xxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxx",
"ClientSecret": "yyyyy-yyyyy-yyyyy-yyyyy-yyyyyyyyy",
"GrantType": "credentials",
"Scope": "scope"
}
Here is the Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
var identityServerSettingsSection = this.Configuration.GetSection("IdentityServerSettings");
services.AddIdentityServer()
// here i need to app those properties from json
}
Here is how i read them from json file
identityServerSettingsSection.GetValue<string>("ClientId")
Thanks in advance!
If you want to read the different attributes of the section IdentityServerSettings you can do it like this, suppose you want to read ClientId.
this.Configuration.GetSection("IdentityServerSettings").GetSection("ClientId");
Or if you want to read all the attributes you can use the option pattern.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1
you can add identityserver like this
services.AddIdentityServer(Configuration);
which Configuration is IConfiguration. and for appsetting.json follow this IdentityServer Options
Related
I wanna hide that i use ASP.NET Core.
The Cookie name show that i use ASP.NET Core and Identity.
So, I Would modify the Cookie name, I don't know way.
How to do this?
Somebody help me please.
I solved this.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "MyCustomCookieName";
});
}
}
This works since .NET Core 3.1.
When using AddMicrosoftIdentityWebAppAuthentication from Microsoft.Identity.Web in .Net 5+, the ConfigureApplicationCookie method didn't work for me. I was able to change the name of the .AspNetCore.Cookies cookie by using the following code placed directly after the AddMicrosoftIdentityWebAppAuthentication method call:
services.Configure<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
options.Cookie.Name = "AuthCookieName";
});
I'd like to add my app's build number to all logs in an ASP.NET Core 3.1 app that is using Application Insights for log storage. Is this possible without having to use BeginScope and EndScope everywhere? I assumed it would be part of the ConfigureLogging startup hook, but didn't see anything. I've done this in the past with Serilog's enrichers, but am not using that library currently.
You can achieve that with TelemetryInitializer. (https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#addmodify-properties-itelemetryinitializer)
public class BuildNumberTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
(telemetry as ISupportProperties).Properties.Add("BuildNumber", "ValueForBuildNumber");
}
You need to add this initializer to the config, which is done like below if you are on Asp.Net Core applications.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ITelemetryInitializer, BuildNumberTelemetryInitializer >();
}
I have an issue where in a class using automatic attributes Json.Net adds k__BackingField to attribute names.
I looked at various recommendation (ie adding [JsonObject]) and none of them works for me.
I found one recommended solution is to do:
((Newtonsoft.Json.Serialization.DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;
(https://stackoverflow.com/a/36910449/460084)
However I don't know where or how to do this in .NET Core 2.1 ? does this goes in Startup.cs ? how ?
Also I am not even sure that will help, as my class does not have [Serializable] to begin with.
Any help ? Is there really no simple way to change the default for Json.Net to use attribute names as is without the k__BackingField in .NET Core 2.1
Add it to the Startup.cs in ConfigureServices method:
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddJsonOptions(options => (options.SerializerSettings.ContractResolver as DefaultContractResolver).IgnoreSerializableAttribute = true);
// ...
}
I am applying authorize attibutes on each classes.
So is it possible to avoid this, and secure my entire web application at once?
Something like at "Namespace" level?
I am using .net core mvc application.
You should add your Authorization filter in ConfigureServices method on startup.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.Filters.Add(typeof(YourCustomAuthorizationAttribute));
});
}
None of the above worked. But I got the solution. So following worked for me.
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
I know this may be late. But ideally, the check must happen only if there is an authorization header found. It is never possible for all pages in a project to require authentication... There must be at least one login page that does not need authentication
I am trying to migrate from .NET Core 1.1 to 2.0, and am stuck migrating the JWT Token configuration. I have an interface/class that provides the JWTBearerOptions, and in .NET Core 2.0 I cannot access my DI Graph objects (since in 2.0, JWT is configured in the ConfigureServices() function). I want to keep my Startup.cs file clean of so many lines of code configuring JWT.
Is there any way to delegate the JWTBearerOptions object creation to a provider created through DI? I want something like the below:
public virtual void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication()
.AddJwtBearer(MyAuthicationScheme.JwtAuthName, options =>
{
myInjectedInstance.SetJwtBearOptions(options, Configuration);
})
}
#flodin
I came across this same problem for the AddJwtBearer and there is a cludgey way to get access to the HttpContext by plugging into OnMessageRecieved Events
jwt.Events = new Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerEvents()
{
OnMessageReceived = context =>
{
// setting the issuer validator delegate here instead of in the jwt.TokenValidationParameters
// allows for accessing the HttpContext items and DI container
context.Options.TokenValidationParameters.IssuerValidator = (issuer, token, parameters) =>
{
// di in callbacks!
var test = context.HttpContext.RequestServices.GetService<ITenant>();
return Task.CompletedTask;
}
Found the answer on microsoft's docs. It is impossible to access DI objects during the ConfigureServices call: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/index?tabs=basicconfiguration#additional-notes