We are using C# to execute a stored procedure where this stored procedure uses EXECUTE AS
to call data from a linked server.
ALTER PROCEDURE [NS_TEST].[SP_GET_DATA_TEST]
@P_VALUE NVARCHAR(100) = NULL
WITH EXECUTE AS 'PROXY_USER'
AS
BEGIN
-- select from linked server
END
When connected to the server in SSMS as a user, it works as expected and pulls data from the linked server.
When executing this in a C# application with a connection string that connects with a as the same user as SSMS before, it fails with being unable to access the linked server.
The OLE DB provider “MSDASQL” for linked server “SERVER_NAME” reported
an error. Authentication failed.
using (var connection = new SqlConnection(_connectionString))
using (var command = new SqlCommand(NS_TEST.SP_GET_DATA_TEST", connection))
{
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.AddWithValue("@P_VALUE", "TESTVALUE");
connection.Open();
await command.ExecuteNonQueryAsync();
}
We’ve tried both Microsoft.Data.SqlClient
and System.Data.SqlClient
with the same result.
Is there a reason that this will work via SSMS and not via the C# code?