Accessing shared folders on other servers from ASP.NET Core applications (IIS, application pool settings)
Operation verification environment
- Visual Studio
-
- Visual Studio 2022
- ASP.NET Core
-
- 6 (Razpr Pages)
- Windows Server
-
- 2022 (ASP.NET Core System Requirements)
- 2019 (Shared Folder Deployment Server)
- IIS
-
- 10.0
Operating environment
I haven't tested it in everything, but it should work for the most part.
- Visual Studio
-
- Anything that can develop a ASP.NET or ASP.NET Core project
- ASP.NET Core
-
- Any version (MVC, Razor Pages, API)
- ASP.NET
-
- Any version is acceptable
- Windows Server
-
- Windows Server 2008 or later
- IIS
-
- 7.0 or later
precondition
- ASP.NET Core applications are intended to run on IIS. Other web servers will not be able to implement the contents of these tips
environment
It is verified in the following environment.
Purpose of use of | PCs and servers |
---|---|
Windows 11 (Local) | An environment for developing programs. Irrelevant to the purpose of this article |
SV2022Test | An environment that runs IIS and ASP.NET Core. Access the SV2019Test shared folder from here |
SV2019Test | Servers with shared folders |
In addition, the various settings are as follows.
Parameter Name | Value |
---|---|
Access Username | SharedUser |
Shared Folder Name | SharedFolder |
Create a program to read and write files from a shared folder from a ASP.NET Core application
As a Mr./Ms. pull, simply click the button,
- Load files in a shared folder and display them on the screen
- Write a new file to a shared folder
process.
I haven't created a shared folder yet, so I can't debug it, but it's simple code, so I'll just make a program for the time being.
ASP.NET Core project can be any type, but in this case, I'm using Razor Pages.
Index.cshtml.cs
and add what happens when the button is clicked.
The server name is written directly, but please adjust it well in actual operation.
In some cases, the shared folder may not be accessible, so it is enclosed in a try-catch.
public class IndexModel : PageModel
{
// 省略
public void OnGet() { }
// ここから追加
public void OnPost()
{
var serverName = "SV2019Test";
try
{
var readFilePath = $@"\\{serverName}\SharedFolder\Input.txt";
var writeFilePath = $@"\\{serverName}\SharedFolder\Output.txt";
// 共有フォルダからファイルを読み込む
var text = System.IO.File.ReadAllText(readFilePath);
// 別ファイルとして共有フォルダに書き込む
System.IO.File.WriteAllText(writeFilePath, text);
// 読み込んだ内容を画面に表示する
ViewData["Message"] = text;
}
catch (Exception ex)
{
ViewData["Message"] = ex;
}
}
// ここまで追加
}
Index.cshtml
has a button and a message.
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
@* ここから追加 *@
<form method="post">
<button type="submit">処理実行</button>
</form>
<div>@ViewData["Message"]</div>
@* ここまで追加 *@
Once you have created the program, create a file to deploy to the server. In this case, we will use the method of placing the published file directly, but it does not matter which method you use. In addition, the arrangement of the program is not the essence of this tip, so it is listed in a simplified manner.
Add a publishing configuration.
Change the settings.
The settings are changed as shown in the figure. This is also not particularly related to the purpose of this time, so please set it according to your environment.
Publish the program.
After publishing the program, click on the "Target Location" link and the folder with the files you want to place will open.
Building a Shared Folder Server
Create a user
Normally, if the server is different, the user created on each server is internally treated as a completely different user. By creating a file with the same user name and password, you can skip Windows authentication to another server. This time we will use this to access the shared folder on another server.
By the way, if you are dealing with servers and accounts in a domain such as Active Directory, you can set up both servers with that account, which simplifies the configuration to some extent.
The procedure for creating a user is not described in detail.
SharedUser
In this case, we'll create it with the name .
Since this user does not operate the screen or change the settings, the password cannot be changed.
If you leave the default, you can log in with this user with Remote Desktop, etc., so please remove from the group Users
.
Creating a Shared Folder
It doesn't matter where you create it. This is because other servers do not care about the location of the physical folder.
In this case, we will create a folder named directly under SharedFolder
the C drive and share it.
Open the properties and configure the sharing settings.
The name of the shared folder should SharedFolder
be . This name will be visible to other servers.
Add SharedUser
in the permissions.
Everyone
Delete the existing .
Confirm with the "Change" permission.
Since we have only added permissions that can be accessed from the outside, we will set it internally SharedUser
so that can operate in this folder.
Confirm with the "Change" permission.
Create a file to check the operation.
It is OK if you can access in Explorer from \\<サーバー名>\
another PC, logSharedUser
in with , and view the file.
Building an Application Server
Installing IIS
For the time being, install it by default from the server manager. I won't go into the details of the procedure.
No additional features are required.
No additional IIS services are required at this time.
ASP.NET Core Runtime Hosting Bundle Installation
Since we are using ASP.NET Core 6, we need to install the runtime accordingly. Download it from the following URL:
In order to run ASP.NET Core in IIS, you need something called "Hosting Bundle". Download the "Hosting Bundle" from the ASP.NET Core Runtime.
Once downloaded, run it on the server.
Follow the wizard to install it.
Creating and Deploying Web Applications
From Windows Administrative Tools, open Internet Information Services (IIS) Manager.
You can use "Default Web Site" as it is, but this time we will create a new site. The Default Web Site is automatically started, so please stop it.
Create a new Web site.
This time, the site name is SharedFolderAccess
, but it doesn't matter.
The application pool is auto-generated for the new site.
Create and specify a folder of your choice where you want to place the program.
Port 80 is labeled "Default Web Site" and you will see a warning, but you can ignore it if it is already down.
It has been created.
Open the folder specified in the Web site in Windows Explorer, and copy and place all the published programs as they are.
Open the page from the IIS link and see if the screen appears. You can open a web browser first and enter the URL directly.
By the way, even if you click the button in this state, you will get an error because the permission setting has not been completed yet.
Create a user
Create the same user SharedUser
on the application server as on the shared folder server. Make sure your passwords match as well. The steps are exactly the same.
Changing Application Pool Permissions
Access by Web programs is determined by the application pool.
By default, it has only ApplicationPoolIdentity
the privileges to run web applications.
By changing this privilege to SharedUser
, the web program can operate with the privileges of the user you have set.
This allows web programs SharedUser
to access shared folders in .
When you open IIS Manager, select Application Pools.
There is an automatically added SharedFolderAccess
when you create a website, so right-click on it and select "Advanced".
There is an "ID" in the process model, so click the button on the right.
Select "Custom Account" and click the "Settings" button.
SharedUser
Enter and confirm your username and password.
Confirmation of operation
Go to the web page and click on the button. If the contents of the text file are displayed, it is successful.
Make sure that you also have a new file in the shared folder.