mardi 27 janvier 2015

Strange Behaviour While Connecting to Database using SPSecurity.RunWithElevatedPriviledges



I am a beginner in Sharepoint.


I have a stored procedured which needs to be executed when the user clicks on a button on the sharepoint page.

The Click event is firing as expected but I get an error saying "NT Authority\Anonymous Login` does not have the required permission.

Below is the code for calling the SP



SPSecurity.RunWithElevatedPrivileges(() =>
{
//Impersonate using Windows token
using (WindowsImpersonationContext ctx = SPSecurityContext.GetWindowsIdentity().Impersonate())
{

_connectionString = @"Data Source=MyServer\USERDATA;initial catalog=MyDBName;integrated security=True";
ExecuteSP();
}
});

private void ExecuteSP()
{
using (SqlConnection con = new SqlConnection(_connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("MySPName", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("parameter", System.Data.SqlDbType.Int).Value = 1;
cmd.ExecuteNonQuery();
}
}


It fails at con.Open()


I know 100% for sure that the ctx does get the Context of the user who has logged in my application but somehow it fails while connecting to the DataBase.


This code runs perfectly on my Dev environment but fails on the testing system.


I have tried moving the entire code within RunWithElevatedPriviledges but still no luck :(


Any help/ideas?








0 commentaires:

Enregistrer un commentaire