I'd like to know under what IIS' Application Name my asp.net core 3 application is running at start time. Possibly also Site Name.
There is a misleading IHostingEnvironment.ApplicationName which is something else (assembly name in fact).
Any idea?
As far as I know, there is no build-in method which could get the web application name. Here is a workaround. We could try to use System.Security.Principal.WindowsIdentity.GetCurrent() to get the current identity name.
Normally, the identity name is the IIS web site name if you don't modify it.
Details, you could refer to below codes:
public IActionResult Index()
{
string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
return View();
}
Result:
Notice:Remove the IIS application pool is the IIS web site name
Related
I want to get current windows user name when user opens the website. My application uses Blazor server-side. To get current username, I added:
In startup.cs:
services.AddHttpContextAccessor(); (under
ConfigureServices)
In razor page:
#inject IHttpContextAccessor httpContextAccessor
In razor page method:
string userName = httpContextAccessor.HttpContext.User.Identity.Name;
When I execute the code, the application throws an exception:
"An error has occurred. This application may no longer respond until reloaded."
I get this error only when I deploy on IIS. On local machine it works well.
I had a similar issue trying to access HttpContext for user information using Blazor. I found this here which was a life saver. HttpContext is NULL when running web app in IIS
All I had to do to solve the problem was to enable WebSockets in IIS.
Here are the steps: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-3.1#iisiis-express-support
If you were to change that string from
string userName = httpContextAccessor.HttpContext.User.Identity.Name
to
string userName = httpContextAccessor?.HttpContext?.User?.Identity?.Name??"Anonymous"
then it would probably work.
It's because your IIS settings are allowing anonymous access and so your Identity object is null.
You should check for the inner exceptions behind the application crash but that will be your issue. You need to prevent anonymous access on IIS to get the Windows authentication passed through.
To manage IIS refer here https://stackoverflow.com/a/5458963/12285843
Hope you all are fine.
I'm working on a project that based on vue-js (FRONTEND) and .net core web api (BACKEND).
The issue I'm facing is I've to get the client IP-ADDRESS using that C# dll which is located in Server. I don't know how to get it.
Please let me know if any of you know.
Will be highly appreciated.
The requester's IP address can be found on the Connection value of the HttpContext for the request, as RemoteIpAddress. For example, in an ASP.NET Core controller action:
public IActionResult MyAction()
{
IPAddress? ipAddress = Request.HttpContext.Connection.RemoteIpAddress;
return Ok();
}
As noted in docs, this might be null.
Our application (Server-side Blazor in .NET Core 3.1) runs within IIS on a Windows Server. We have multiple sites in IIS running the same application but with different URL's for different customers.
At startup in (ConfigureServices) we want to load customer configuration for the application from a config file. That way we can have multiple instances of the application running with different configs. Loading this information from the database is not an option because the config contains the details to connect to the database.
In ASP.NET Framework we would have access to the virtual path or (sub)domain name in the Global and then load the configuration based on that information.
We need the same access in our ASP.Net Core applications or another work around.
Is there any to achieve the same result?
A better way to distinguish sites is by URL. The domain name and port bound to each site in IIS will not be repeated.
You can refer to these code to get URL in startup.cs.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,IHostApplicationLifetime lifetime,IServiceProvider serviceProvider)
{
...other code...
lifetime.ApplicationStarted.Register(
() => logAddresses(app.ServerFeatures));
}
static void logAddresses(IFeatureCollection features)
{
var addressFeature = features.Get<IServerAddressesFeature>();
if (addressFeature != null)
{
foreach(var address in addressFeature.Addresses)
{
Console.Out.WriteLine(address);
}
}
}
I was able to grab the Application Pool ID from the Environment and then load the config section as the application config:
var appPool = Environment.GetEnvironmentVariable( "APP_POOL_ID", EnvironmentVariableTarget.Process )
van sectionname = SomeMagicToParseAppPool( appPool );
var config = Configuration.GetSection( sectionName );
if ( config == null ) throw new ApplicationException($" Cannot find config section for {host}");
services.Configure<ApplicationSettings>( config );
This allows me to load a different config for a different site. The only downside is that each application requires their own Application Pool. But that was already a requirement due to a .NET Core ASP.NET app having to run unmanaged in the Application Pool.
I have 2 projects in my solution. First one is simple mvc project and the other one is web api. There was no pre-written code in web api. I put all logics myself. Now I want to add asp.net identity in the web api project. How can I do that?
Thanks.
In your web api project, you can do this:
1. Create a DbContext class that looks like this:
public class DataContext : IdentityDbContext<IdentityUser>
{
public DataContext() : base("ConnectionStringLocal") { }
}
Add a connection string in your Web.config file
In Package manager console, do Enable-Migrations, Add-Migration IdentityUpdate, Update-Database. This will create a database that has asp.net identity built in.
Please let me know if you have additional questions.
I created a brand new MVC 4 Project (using Web Api Template, not Internet template). How can I initialize the simple membership database at first run exactly as the internet template does? What I did so far:
created brand new MVC 4 Project -> Web Api Template
implemented the filter InitializeSimpleMembershipAttribute.cs and decorated the HomeController with it.
added the model AccountModels.cs
added enabled="true" attribute to roleManager under system.web in web.config
the connection string is pointing to SQL Azure and working fine
When I run this app for the first time, the database is created but are created just 1 table in it: UserProfile, all the other tables: webpages_Membership, webpages_Roles ... are missing. There are no errors nothing.
All I need is to be able that when my MVC4 Project (Web Api Template) runs for the first time, will ensure that the SampleMembership db and tables are created. In order to be able to authenticate a client rest call using membership authentication.
Please let me know if this is the right path.
Have you checked if the connection string name in the web.config is defined
in AccountModels.cs for the usercontext
in InitializeSimpleMembershipAttribute.cs for the database initialization