简体   繁体   中英

Can not connect to Sql database in C# asp mvc application with Entity Framework BUT the same code works with Winform application

First of all, please excuse me if it sounds too rooky. I consider myself novice in MVC applications. I have ran into a strange problem and there does not seem to be any way out of this, at least so far..I have looked everywhere and left no stone unturned to get it worked. Finally I turned to this forum. This is my first post, so any mistakes please overlook and guide me.

The problem is multifaceted...

The High Level Details... I have created a C# ASP MVC Web Application waiting to be uploaded on a Remote Server (Client Machine) The application uses Entity Framework - Code First approach Connects to the database with Windows Authentication system

Scene1: Where the application worked I have tested the application on my machine and it worked flawlessly. I have Express edition of Sql Server Management Studio installed on my system.

Scene2: Where the application failed. The Problem - Big Picture It works great on my system but while testing it on the Remote Server it crashes The application fails to connect to the Remote Server Sql Database. As soon as it tries to connect to the database, it crashes with an error message "Login failed for user '<UserName>."

I have checked everything in the connection string - like - Data source name is correct Initial Catlog also points at the correct database name Integrated Security = true There is no UserID or password mentioned

Connection string worked great on my system. But it does work on the Client Machine and shows the error above.

Connection string is: connectionString="Data Source=RemoteComputerName;Initial Catalog=DatabaseName;Integrated Security=True; MultipleActiveResultSets=true"

I am not able to figure out exactly what is causing the error - Is it my code or Is it the Sql Server database settings - permissions. Since the connection worked on my local machine, which means the code is correct

In order to check whether sql server permissions are working..I have created partial 'test connection application' in WINFORM and uploaded on the Server, this time the code works and read all the table data.

but when I try to connect in MVC project it shows the error..."Login failed for user...".

I'm totally confused what works in WINFORM fails in MVC.

Database permissions must be right because when tried to access it using WINFORM it worked.

please let me know if I have missed to provide any details in this post.

Any help is highly appreciated!!!

Thank You.

Please show your connection string. (you can block out any real passwords, actual server names, actual db names).

Since you mention "Integrated Security = true"..........then you have to be aware of which Identity is running the process (the website or the winforms.exe)

When running as winforms app, you are running as "you" (most likely). As in, the logged in user to the windows o/s.

When running under IIS, you are running under the Identity associated with the App Pool you are using..which most likely is NOT you, but rather a service account.

Your service-account does not have access to the sql-server.

You can read in depth here: https://support.microsoft.com/en-us/help/4466942/understanding-identities-in-iis#:~:text=ApplicationPoolIdentity%3A%20When%20a%20new%20application,also%20a%20least%2Dprivileged%20account .

You can show this......by catching an exception, and then something like this:

You can replace ArithmeticException with whatever, I'm just showing "adding info" to a caught exception and rethrowing.

try
{
// some sql server access code
}
catch(Exception ex)
{
       throw new ArithmeticException("What is the real IIdentity: " + this.FindIIdentity(), ex);
}


private string FindIIdentity()
{

    try
        {

        //'Dim user As WindowsPrincipal = CType(System.Threading.Thread.CurrentPrincipal, WindowsPrincipal)

        //'Dim ident As IIdentity = user.Identity

        string returnValue = string.Empty;

        WindowsIdentity ident = WindowsIdentity.GetCurrent();

        returnValue = ident.Name;

        try
            {

            returnValue += " on " + System.Environment.MachineName;

        } catch (Exception ex)
            {}

        return returnValue;

    } catch (Exception ex)
        {

        return "Error Finding Identity";

    }

}

IIS screenshots

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM