Adding an AuthorizationHandler via config - web-config

In WCF, you can add an authorization policy through the web.config by using the serviceAuthorization node in a service behavior. Is there a way to include an AuthorizationHandler in .NET Core WebAPI via config?
To be clear, I'm trying to replace this line in Startup.cs with something in the web.config:
services.AddSingleton<IAuthorizationHandler, MyAuthorizationHandler>();

web.config is only used for IIS specific config. Because of .net-core's cross platform nature they ditched coupling to web config for application configuration.
A web.config file is required when hosting the app in IIS or IIS Express. Settings in web.config enable the ASP.NET Core Module to launch the app and configure other IIS settings and modules.
Reference Configure an ASP.NET Core App: The web.config file
Startup is your entry point into the application where you can have some settings in the json file and have your code add/update the configuration based on that.
My thinking is that it would save having to recompile every time you want to add something because configuration options allows you to Reload configuration data with IOptionsSnapshot
Requires ASP.NET Core 1.1 or later.
IOptionsSnapshot supports reloading options with minimal processing overhead. In ASP.NET Core 1.1, IOptionsSnapshot is a snapshot of IOptionsMonitor<TOptions> and updates automatically whenever the monitor triggers changes based on the data source changing. In ASP.NET Core 2.0 and later, options are computed once per request when accessed and cached for the lifetime of the request.
Your authorization handler(s) would depend on the options and perform its function based on the configurations provided.

Related

Is there a way to automatically map existing web.config settings if I'm deploying an existing Web App to Azure?

I have an existing ASP.Net MVC app that's currently hosted locally in IIS and I'm trying to deploy it to an Azure App Service.
I've seen several posts about setting the Applications Settings and Connection Strings manually in the Application Settings blade.
The problem with that is that I have a ton of App settings in my web.config file, along with various other config sections.
Is there a way to automatically map those to Azure Application Settings when I deploy my app?
Your best bet is to write a simple transform tool that changes those web.config based settings into the equivalent ARM template that sets those settings and connection strings (probably < 20 lines of code).
Note that there is no Azure equivalent of sections other than App Settings and Connection Strings.

Is there an alternate to Web.config Transormations in Asp.NET Core appsettings.json?

Is there an alternate to Web.config Transormations in Asp.NET Core appsettings.json?
I would like to change connection strings and other settins when deploying to staging.production server from VS.
First of all, keep in mind, that
ASP.NET Core’s configuration system has been re-architected from previous versions of ASP.NET, which relied on System.Configuration and XML configuration files like web.config. The new configuration model provides streamlined access to key/value based settings that can be retrieved from a variety of sources.
To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration in your application’s Startup class. At its simplest, Configuration is just a collection of sources, which provide the ability to read and write name/value pairs.
ASP.NET Core provides built-in support for JSON, XML, and INI configuration files as sources and allows to choose source accordinly to current environment. The ASPNETCORE_ENVIRONMENT environment variable is used to setup/determine the current environment. Predefined values are Development, Staging, Production, but can be any custom also.
Lets say you want to use JSON file (named appsettings.json for convention) for storing connection string. What you need to do is to create a separate appsettings.{EnvironmentName}.json files (for each environment you have) like:
appsettings.Development.json
appsettings.Staging.json
appsettings.Production.json
and load one of them accordingly to environment:
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
Read more about all of this in the “Configuration” chapter and the “Working with multiple environments” chapter of the ASP.NET Core documentation.
Yes, you must use environment variables on the target system.
ASP.NET Core references a particular environment variable, ASPNETCORE_ENVIRONMENT to describe the environment the application is currently running in. This variable can be set to any value you like, but three values are used by convention: Development, Staging, and Production. You will find these values used in the samples and templates provided with ASP.NET Core.
Determining the environment at runtime
The IHostingEnvironment service provides the core abstraction for working with environments. This service is provided by the ASP.NET hosting layer, and can be injected into your startup logic via Dependency Injection. The ASP.NET Core web site template in Visual Studio uses this approach to load environment-specific configuration files (if present) and to customize the app’s error handling settings. In both cases, this behavior is achieved by referring to the currently specified environment by calling EnvironmentName or IsEnvironment on the instance of IHostingEnvironment passed into the appropriate method.
https://docs.asp.net/en/latest/fundamentals/environments.html

how to solve SOAP services issue with portable class library?

i have an issue using SOAP services from portable class library.
once i add the service refrence configuration file created empty and calling any operation form Windows Phone or Windows store app project returns null value.
However if i added the refrence to WP or W8 project directly configuration file is npot empty and operations return data
any reason for that ?
It is the same as in full .NET. If you call a web service, the configuration file is searched for in the calling assembly, that is in your case the WP8 project.
You have two options. One is to copy the relevant configuration from app.config in the PCL to the WP8 porject or to create the web service configuration completely in code in your PCL so no config file anywhere is needed.

How to mention Expire Header and Compression for IIS 6 in web.config

I need to specify the Expire Header and enable Compression for static files such as images, css and js in order to optimize my site. My web app is hosted in IIS 6 with shared server. Since, this is a shared server, so I don't have access to IIS manager. So, the only option, I have is play with "web.config".
Technologies which I am using is Asp.Net MVC 2.0.
Thanks.
Not possible in IIS 6 through Web.config, since config support for such operations were added in IIS7. Ask for assistance from your hosting service provider.

Template ASP.NET MVC2 Web Application

I’m new to MVC and am trying to set-up a basic template application. From VS2010 I’m creating the template MVC2 project. However, I’ve noticed that the database is sometime not created as part of the template. Are there any rules to when or where this creates its own database, or why it might not do so?
The standard MVC template has a connectionstring in the web.config pointing to a aspnetdb.mdf database in the App_Data folder.
The default MVC 2 Web Application template uses the database for authentication (through the AccountController).
The physical file gets created on first use; probably when you register an account or attempt a logon while running the application.