I tried to create ASP.Net Web Site solution with the same code that working properly so I am sure that the problem is not in code and it’s related to SharePoint Web Part.
I tried to check Authentication of SharePoint Site by following :
Start–> Administrative Tools –> IIS Manager then navigate to my SharePoint site then select Authentication .
At ASP.Net Impersonate it’s Enabled
When I set Asp.Net Impersonate to false it’s worked properly
Unfortunately this solution affect on some features in my farm like search where I tried to run a full crawl search from central administration that not working :(
I tried to check the Crawl Log where I got the following error:
error hresult fail has been returned from a call to a com component
I went back to Enable Asp.Net Impersonate , then I ran a Full Crawl search that working properly.but the main error back again
The request failed with HTTP status 401: Unauthorized.
I decided to disable Asp.Net Impersonate at the moment that I show my report therefore i will disable it via the following code in my web part.
public System.Security.Principal.WindowsImpersonationContext impersonationContext; impersonationContext = System.Security.Principal.WindowsIdentity.Impersonate(System.IntPtr.Zero); //Insert your code that runs under the security context of the authenticating user here. impersonationContext.Undo();
If you have another solution please comment it.