Posted on

How to Deploy ASP.NET Core Web API on IIS Windows Server

(Tested on windows server 2012r2)

Install the .NET Core Hosting Bundle on Windows Server

The .NET Core Hosting bundle is an installer for the .NET Core Runtime and the ASP.NET Core Module. The bundle allows ASP.NET Core apps to run with IIS.

Current version:.NET Core Hosting Bundle installer (direct download)

(for this test we installed version .Net Core 8 on win 2012r2)

Visual Studio : Publish on Folder

After creating the ASP.NET Core application in Visual Studio, we can use the Visual Studio Publish Tool to deploy and run our app. For this project, choose to publish to a folder. Choose the folder and hit “Finish”.

In the next screen, where you see the settings for this deploy, click on “More Actions” and then on “Edit”.

Select :

  • Deployment Mode : Complete
  • Target Runtime : win-x64 (for our server)
  • File Publish Options : Delete all existing files prior to publish (flagged)
  • Database : Default Connection edit (if yuu need it)

Save this configuration, control it an Publish.

Copy the contents of the folder on the IIS server to the folder dedicated to the new site.

IIS and new site

Create the site on IIS. For the Application Pool you have to use default .NET CLR Version : v4.0.

Open your browser and call up the site. You receive the “page not found” error (404).

Remember that a site that hosts only calls web api. To verify that it works you can use the controller that Visual Studio sets by default when creating a site. Then type:

https://www.mynewapisite.com/WeatherForecast

and you will get a result. The site works!


Posted on

Error from Filezilla Client to Microsoft IIS FTP Server

When you connect to ftp server create with Microsoft IIS using Filezilla Client you should have this error

GnuTLS error -48: Key usage violation in certificate has been detected. Could not connect to server

Your configuration settings are something like this :

  • Protocol: FTP – File Transfer Protocol
  • Encryption: Require explicit FTP over TLS

The problem is with self signed certificate on server side. This is a problem with the certificate generation of Microsoft IIS, as it does not allow the certificates to be used for digital signatures.

How to generate a valid certificate with IIS

This is a server-side issue, and it did not appear previously because earlier versions of FileZilla shipped with a GnuTLS version that didn’t make this check.

Quoting Tim Kosse’s post in the FileZilla forum thread:

In any case, the problem is with your server’s X.509 certificate chain: Either the server certificate itself or another certificate in the chain has a key usage restriction that is violated. For example a certificate with a key usage restriction to signing cannot be used to authenticate TLS connections. See section 4.2.1.3 of RFC 5280.

This is a problem with the certificate generation of Microsoft IIS (but may also happen if you incorrectly generated a certificate with another method), as it does not allow the certificates to be used for digital signatures. OpenSSL is much more relaxed about this and won’t fail because of it, so it may work with other apps.

On the client side, you can either disable TLS, downgrade to an earlier version of FileZilla (neither of these is recommended due to potential security risks), or use a different client which uses another library such as OpenSSL for now.

How to generate a valid certificate with IIS

This needs to be done on the server side, Yobviously.you can generate the certificate with PowerShell instead until the issue is fixed by Microsoft. Open PowerShell in admin mode.

The following powershell command will create our self-signed certificate for our binding and store it in the Personal Store (Note how I also store a reference to the certificate in a variable called $cert this will be needed further on):

$binding = "192.168.1.70"
$cert = New-SelfSignedCertificate -DnsName "$binding" -CertStoreLocation "cert:\LocalMachine\My"

However, this is not enough to make the certificate work for HTTPS in our browser. We need to add our newly created certificate to the Trusted Root Certificate store. To do this we take our $cert variable which references our created certificate and add it to our Trusted Root Certificate store like so:

$DestStore = new-object System.Security.Cryptography.X509Certificates.X509Store([System.Security.Cryptography.X509Certificates.StoreName]::Root,"localmachine")
$DestStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$DestStore.Add($cert)
$DestStore.Close()

Now you have to set the new certicate on your ftp site using IIS Admin.

Posted on

iis error 0x80070021 on web.config

Questo errore si presenta installando un sito con IIS in locale su un windows 10 o un windows 8.1. Non è escluso che si possa manifestare anche sui sistemi operativi server.

Dettaglio Errore

Modulo
IIS Web Core

Notifica
BeginRequest

Gestore
Non ancora determinato

Codice errore
0x80070021

Errore di configurazione
Impossibile utilizzare la sezione di configurazione in questo percorso. L’errore si verifica quando la sezione è bloccata a livello padre. Il blocco avviene per impostazione predefinita (overrideModeDefault=”Deny”) o è impostato esplicitamente da un tag di percorso con overrideMode=”Deny” o con il precedente allowOverride=”false”.

File di configurazione
\\?\C:\inetpub\wwwroot\test\web.config

Percorso fisico
C:\inetpub\wwwroot\test\miofile

Metodo di accesso
Non ancora determinato

Utente accesso
Non ancora determinato

SOLUZIONE

Dovete installare anche .NET e gli strumenti di sviluppo di IIS

oppure

Posted on

IIS redirect from a site to another site

We must redirect the pages of a site on the pages of another site by changing the main address of the site and keeping the relative addresses of the pages the same. So :

http://blog.mecdata.it/default.aspx will be redirect in https://www.mecdata.it/default.aspx

On our server we need to install an additional IIS module: “HTTP Redirection“.

installing http redirection

in IIS, in your site (in this example blog.mecdata.it) select Http Redirection module and compile it in this way

this configuration will create a web.config file in your site root :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpRedirect enabled="true" destination="https://www.mecdata.it" exactDestination="false" childOnly="false" />
    </system.webServer>
</configuration>

That’all

more information : https://manage.accuwebhosting.com/knowledgebase/988/How-to-set-website-redirection-from-IIS-7.html

Posted on

Spostare un vecchio sito aspx su un nuovo IIS

Ecco i problemi più comuni che si possono presentare durante lo spostamento di un vecchio sito aspx su nuovo web server Internet Information Server.

APPLICATION POOL

Questo problema lo avrete sicuramente quindi sistemate prima di far partire il sito. Quando create un nuovo sito su IIS questi crea un Application Pool separato per il sito ma lo basa sulla versione 4.0 del Framework .net. La vostra applicazione non funzionerà. Dovete selezionare l’Application Pool e modificare la versione del Framework sulla 2.0. A questo punto fate un Recycle dell’Application Pool e un restart del sito.

PROBLEMI con il defaultDocument

Se vi viene segnalato un problema della definizione della default.aspx sul web.config nel tag

<defaultDocument>
            <files>
                <add value="Default.aspx" />
            </files>
</defaultDocument>

Vi basta inserire prima della dichiarazione add il tag clear, come di seguito :

<defaultDocument>
            <files>
                <clear />
                <add value="Default.aspx" />
            </files>
 </defaultDocument>
Posted on

ASP.NET MVC: PUBBLICAZIONE DI UN SITO

Visual Studio ci consente di pubblicare il nostro sito web su un web server interno (intranet) o su un web server esterno, per esempio ospitato su Azure.

Nel caso di un server in lan, su un web server avete creato tramite IIS un sito che risiede su una certa directory per adesso vuota. Altrimenti avete già pronto un sito esterno.

In Visual Studio, nel pannello a destra chiamato “Solution Explorer” facciamo click con il tasto destro del mouse sul nostro progetto e selezioniamo l’opzione “Publish“. Si apre la finestra “Publish” che riporta a sinistra i 4 step da fare per la pubblicazione.

Profile

Andate sul menu a tendina in alto al centro  e scegliete “New Custom Profile”. Inventatevi il nome di questo profilo. Una volta inserito, passerete direttamente allo step successivo.

Connection con il metodo “Web Deploy”

Publish Metod : scegliere Web Deploy sia che si pubblichi il sito in un server in lan, sia in un server esterno.

Server (web server in lan) : http://myserver

Server (esterno, per esempio Azure): mysite.azurewebsites.net:443

Site name (web server in lan) : nome della directory in cui deve risiedere il sito

Site name (esterno, per esempio Azure) : nome che avete dato al sito (mysite)

User Name e Password (web server in lan) : mettere uno username amministratore della macchina server. Se la macchina è in dominio usare un Amministratore locale.

User Name e Password (esterno) : ftp user

Destination Url : indirizzo del sito web. es : http://……………..

Settings

In questa tab il programma legge dal we.config le stringhe di connessione al database. Se nel vostro sito di destinazione il database è diverso, dovete specificare la giusta connessione. La pubblicazione sostituira le stringhe di destinazione alle stringhe di origine. Vedremo in seguito che esiste un metodo per inserire nel web.config ulteriori variazioni rispetto al web.config di origine.

Preview

Qui potete vedere i file che saranno inviati alla destinazione cioè i nuovi o  i modificati dopo l’ultimo invio. E potete pubblicare!


Configurazione del web.config

Il nostro sito web di destinazione può avere altre caratteristiche particolari, specificate nel web.config, diverse dal sito di partenza, oltre alle stringhe di connessione al database.
Ci possiamo creare un web.config specifico per ogni deploy.

Andate sul progetto. Nelle “Proprietà”, trovate la directory “PublishProfiles” che contiene i dati salvati precedentemente. Trovate un file .pubxml per ognuno dei deploy che avete creato. Andate su file che vi interessa, spingete il tasto destro del mouse e selezionate “Add Config Transform”.

Se andate nel web.config vedrete che esiste un file aggiuntivo che si chiama come il vostro deploy.  Vediamo alcuni esempi di quello che potete fare con tag che inserirete all’interno di <configuration>

<connectionStrings>
 <add name="MyDB"
 connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
 xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
 </connectionStrings>

Questa stringa sostituisce (xdt:Transform=”SetAttributes”) gli attributi del tag che si chiama (xdt:Locator=”Match(name)) “MyDB” con quelli che trova qui.

<appSettings><add key="myemail" value="info@contoso.com" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/></appSettings>

Questa stringa sostituisce gli attributi (xdt:Transform=”SetAttributes”) del tag che ha come key (xdt:Locator=”Match(key)) “myemai” con quelli che trova qui.

<system.net>
 <mailSettings xdt:Transform="Replace">
 <smtp deliveryMethod="Network" ........................
 </smtp>
 </mailSettings>
 </system.net>

Questa stringa trasforma (xdt:Transform=”Replace”) tutto il tag mailSettings ed il suo contenuto.