Forms Authentication & ASP.NET Membership Session Timeout - c#

I have a website which has Forms Authentication and ASP.NET Membership is used in it. At some pages Transactions (System.Transactions) are used too.
After deploying it to the server, the session timeout issue is creating a mess, and few E-mail relays and DB operations are unable to be executed.
I want to know how can I fix this issue. If additional information is needed I can provide.
I have looked into too many blogs and forums, but could not find the solution or the correct/single solution.
Following error is shown sometimes:-
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.
And the application is deployed on Windows 2008 R2 Server with IIS 7.5.
Looking forward for quick response.

Sounds like you're not closing your DB connections correctly.
Make sure any place you open a SQL connection that you're closing it. Ideally wrap them in using blocks.
Post up some some of your typical DB code and I'll review it.

Related

Why does .Net try to log in HTTP/computername into SQL Server(Windows) when using Kerberos from Linux?

I am at the end of my rope and need some help. This will probably turn out to be something simple but I have been debugging the code and reading the same web pages for so long, I probably missed it.
I am working on setting up an environment so we can run .NET Core APIs from a Linux server. These will hit an SQL Server running on Windows for data and use Windows AD for Integrated Security. The outfacing server is Apache with .Net running reverse proxy using the standard Kestrel. So I have:
client browser -> Apache(Linux) -> Kestrel(Linux) -> SQL Server (Windows Server 2016)
Server FDQNs:
Linux - cats6-87.it.my.company
Windows - cats7-47.ad.my.company
I will be referring to these by short names from here on, but please note that all of the actual settings use the FDQN. It should also be noted that these two servers are NOT in the same domain; the Win server is part of the AD while the Linux server is not.
I am able to use Kerberos to authenticate users in Apache. This was done via the "standard" way by creating a special AD account (catadn001), setting an SPN call HTTP/cats6-87.it.my.company, creating a keytab, and setting up apache to use the account and tab. I am also able to pass this login to my .Net application and the data shown in HttpContext.User indicates the user is authenticated.
I am trying to double-hop so the DB can be hit by the authenticated user. I set up the environment according to every piece of documentation I could find:
SPNs of MSSQLSvc/cats7-47:1433, MSSQLSvc/cat7-47:CATS747
catadn001 is set to be able to delegate Kerberos for MSSQLSvc
The SQL Server log indicates the SPNs are set up correctly and running SELECT distinct auth_scheme FROM sys.dm_exec_connections after logging in using Windows Authentication in SSMS from a Windows 10 machine returns KERBEROS.
Back to Linux.
Using kinit I am able to retrieve a ticket for HTTP/cats6-87. klist confirms this.
In my .Net application, I am using Microsoft.Data.SQLClient to try to open an Integrated Security connection in an API endpoint:
new SqlConnection("Data Source=cats7-47;Integrated Security=True")
As I am experimenting here, I run my test application as myself. Before I had even tried to hit my endpoint though, I had a couple of questions:
How would .Net know to use HTTP/cats6-87?
How would the credentials passed in from Apache be passed along to SQL Server?
The SqlClient documentation does not mention it. In any case, from the few pages I found about Integrated Security from Linux to Windows and SQLClient, the above connection string should be enough. So I tried it.
No joy.
The error message presented was Login failed for user HTTP/cats6-87. I checked the SQL Server log and confirmed this. My application is trying to login with the Kerberos delegation account. I tried to search for this and every single page came back about error messages for anonymous login indicating problems with SQL Server and Kerberos. But I had already confirmed that SQL Server was using Kerberos correctly.
So I tried stepping through the SqlClient code by using the github repo of it. I learned a lot, particularly about how the user name is derived on Linux versus Windows (WindowsIdentity is not available) and about how annoying OOP truly is, but nothing to solve the problem and I still don't know the answer to my two questions above.
Can someone help? How do I get my linux application to use the Kerberos delegation to log into SQL Server in Windows?

How to Gracefully Shut Down ASP.Net (not Core)

I have a WCF service application hosted in IIS. On startup, it reads the SQL connection information from the web.config and connects to the SQL Server to get the necessary information to bootstrap the application. If the SQL Server is unavailable for some reason, we want the service to shutdown - which I currently accomplish by throwing an exception.
The problem we have with this is that, if a service fails to start more than N times, IIS helpfully turns off the application pool - which means that, after the SQL Server connection issue is corrected (possibly entirely external to the IIS server), a sysadmin has to think to go an turn the app pool back on.
What I'd like to do is have the service gracefully exit. I know that there are ways to do this with ASP.Net Core (i.e., ways to hook into the lifecycle management of the process and tell it to exit), but have been unable to find anything like that for .Net Framework/ASP.Net.
Is there such an API that I can use?
Turns out that this method is available and works.
https://learn.microsoft.com/en-us/dotnet/api/system.web.hosting.hostingenvironment.initiateshutdown?view=netframework-4.8

Continue server side processing on .netcore IIS (migration stuff)

I have a project where users upload multiple 4-6MB AccessDb files to migrate into a AWS hosted SQL Server database. I think what is happening is that IIS maybe shutting down the process after some amount of time.
Its basically a file upload and then a c# static class that reads the mdb file and uses EF .core to copy into SQL Server. Using Visual Studio, i don't see any shutdowns and all test files migrate successfully.
Website built using .netcore 2.1 / c#
I have Web.config timeout set to requestTimeout="00:59:00" so that basically stops some 503 errors.
What I'm seeing is that if all browsers are closed(or maybe not) and maybe technically no connections to IIS, that after a few minutes, the migration stops. I'm not seeing new rows added to the database.
I want my c# .netcore processes to continue migrating whatever databases the user uploaded without IIS or whatever other process killing the connection.
Looking for tips I can update. I'm assuming there's other Web.config settings I can update?
It sounds like your code that processes the files is a background job in your application, so I think I know what's going on.
By default, IIS app pools are configured to shut down after 20 minutes of inactivity (no incoming requests). You need to disable that.
The option is called "Idle Time-out (minutes)" in the Advanced Settings of the application pool. Set it to 0 to disable it.
There are also options in there to automatically refresh the app pool, so review those and make sure they are acceptable to you (I forget what the defaults are).
It's not a good design decision to keep an http connection open for that long. First of all asp.net core will drop the thread after a while, and second the user could drop the connection by mistake. I would recommend doing this async to the user:
Receive the file, validate it.
Schedule a background job using either Hangfire or the builtin
IHostedService interface.
When the migration is complete email or notify the user some other
way.

Session lost in few seconds randomly

My web application is developed using Dot Net 4.0 C# and MS SQL 2008 R2. Application and Database are deployed on separate servers.
I have used form authentication on my application and user session is maintained in Session object. Recently the session object on my application was getting lost frequently because of no reason.
I've tried below stuffs to fix this, but nothing worked.
Increase the session timeout from web.config
Increase the IDLE Timeout of Application Pool from IIS
Set the number of Worker Process to 1
Proper exception handling for required method and global exception handling
Fixed all exceptions and memory leakage.
Because of some other reason we've restarted our SQL Server Machine today and everything started working perfect now.
I'm confused now on how can a InProc session issue be solved by restarting SQL Server machine.

Load balancing OR Failover servers for .net application

Scenario
I have service written in c#.net.
Now on my production environment I want this service to be on 2 different servers for Loadbalancing or failover of one of the server.
So for instance if one server dies the service keeps running on the other server and users are not affected OR if there is a heavy load on the site users get redirected to different servers.
For users getting on redirected on different servers is not a problem we have the software which can check whether the service is running or not. How to do session management in this case?
How can we achieve this?
Of course you could always spin your own session management service but I don't think that is what you are looking for.
I have experience using NCache for session management and it works well enough.
Microsoft also has Velocity but I'm not sure it's complete yet.
Very old question, but still searchable by Google. Since Windows 2012 there is feature called Clustered Shared Tasks. Probably not mature yet as it is manageble only via PowerShell, but maybe good start. Check this MSDN post. I'm eager to try it myself.

Categories

Resources