TFS access problems through c# web app - c#

I'm trying to build a web application in C# that can get the latest version of a file from a tfs server. That file with then be edited and etc...
Right now I get permission errors when trying to download a file using
IIdentity WinId = HttpContext.Current.User.Identity;
WindowsIdentity wi = (WindowsIdentity)WinId;
WindowsImpersonationContext wic = wi.Impersonate();
that code. After that I do a connection to the TFS Team Project Collection using:
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new
Uri("http://tfs:8080/tfs/DefaultCollection"));
which works fine. I can use PendEdit, get the workspace, get pending changes, everything. However I cannot do:
SourceControl.DownloadFile(serverFilePath, localFilePath);
I have tried a local path both in my workspace and in a newly created workspace both with same result
I will always get permission denied (To be exact I get this -> TF30063: You are not authorized to access Microsoft-IIS/7.5.) SourceControl is defined properly and I have full permission to my local folder and on TFS. I can get latest by right clicking the file and saying get latest I just can't do it in the app.
Any suggestions/problems with what I'm doing?
If I can't do this using just the impersonate stuff how do you prompt for credentials each time?

SourceControl.DownloadFile(serverFilePath, localFilePath);
The localFilePath that you are mentioning above, is it same as the Workspace path or some other path? Can you try to mention the workspace path of the file and try the method?

Related

Access to Path Denied - Magick.NET.net40.7.4.3.0

I am trying to read text from images using IronOCR. It worked fine on my dev machine but when I try run it through IIS on another machine I am getting the following error
Access to the path 'C:\Windows\TEMP\Magick.NET.net40.7.4.3.0\Magick.NET-Q8-x64.Native.dll' is denied.
there is no Magick.NET.net40.7.4.3.0 folder in the window temp folder location on the dev machine, but it was there on the other machine but it was empty. I deleted the folder and now it is saying there is access denied on the user app_data folder.
I am totally confused as to why it is looking for access to Magick.NET.net40.7.4.3.0, is there some connection between the 2?
In your code you'll need to set :
IronOcrInstallation.InstallationPath = "d:\newpath"
where d:\newpath is a directory on your IIS that your worker process or IIS has access to.
BTW IronOCR is a commercial re-bundling of the open source Magick.NET image and Tesseract OCR libraries.

Access denied to web server file

I'm working on a web app that runs Excel reports and I'm using an Excel template file that's saved on the server. It works fine when I run it locally but when I try to run the Excel report on the server I get an error saying access is denied to the template file that's saved in the same directory, but I can download the template file when I navigate to it in the browser.
I granted everyone full access to the file to see if that was the issue but I still get the error. What am I overlooking?
error errorId="05d6462e-8bb5-401d-85bc-cd4d9e36fc90" application="/LM/W3SVC/8/ROOT" host="USFLWCT0" type="System.UnauthorizedAccessException" message="Access to the path '\usflwct0\D$\SherlockRoot\Files\Volume_by_Week_Template.xlsx' is denied." source="mscorlib" detail="System.UnauthorizedAccessException: Access to the path '\usflwct0\D$\SherlockRoot\Files\Volume_by_Week_Template.xlsx' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at
edit:
Here's the code used to open the file. The path is hardcoded for now
using (var template = System.IO.File.OpenRead(#"\\usflwct0\D$\SherlockRoot\Files\Volume_by_Week_Template.xlsx"))
You are using one of the admin shares that Windows creates for each drive, in this case the D$ share. They are only accessible by admin level users. You should share the folder you want directly, for example, share the D:\SherlockRoot\Files folder as SherlockFiles that was you can access the content like this \\usflwct0\SherlockFiles\Volume_by_Week_Template.xlsx or in code:
var file = #"\\usflwct0\SherlockFiles\Volume_by_Week_Template.xlsx"
using (var template = System.IO.File.OpenRead(file))
If you want the application to read the the template file, put it in the App_Data folder

TeamCity building with MsBuild and NuGet: "Unable to connect to the remote server"

I have setup my TeamCity to build using MsBuild a project that NuGet packages.
During the build, the following command to install packages is issued, but fails:
..\nuget.exe install "C:\TeamCity\buildAgent\work\811b6866c8757c46\Service\packages.config" -source "https://nuget.org/api/v2/" -RequireConsent -solutionDir "..\ "
Error:
Unable to connect to the remote server
with exit code 1.
Interesting to note is that when I run this exact same command on the cmd prompt (inside the same path), it succeeds without any errors.
This is what I have done so far:
Add a new Build Parameter under environment variables in TeamCity: env.EnableNuGetPackageRestore and set it to 'true'
Add a specific path to the package sources (https://nuget.org/api/v2/) inside the ..nuget\nuget.targets file (as described here)
To provide the additinal paths ways to supply a path:
Modified the nuget.config file inside the .nuget folder (..nuget\nuget.config)
Modified the nuget.config for the SYSTEM account that the build runner is executing under (C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\NuGet\nuget.Config) (as described here)
What I was thinking is that this has something to do with a roaming profile of the System user (that the build agents runs with) because it all works when build agent runs with my account. But the nuget.config is the same for both profiles, and I'm out of ideas. Maybe the System user doesn't have access to the Internet on WinServer2012R2? Maybe it needs additional permissions? Which ones?
Do you have any ideas of what to try?
The error turned out to be the setting for the ISA server we have on our network (the TMG client). By default this isn't set up for new (local) users and therefore the SYSTEM account didn't have access to the web.
I've set this up for a new local user (non-domain, with password that doesn't expire), added it to Administrators group and now it works just fine.

TFS API: GetLocalWorkspaceInfo always returns null

On one of my machines, I get a return value of null from any GetLocalWorkspaceInfo call. I have isolated to problem to where it even fails for this simple program:
namespace WorkstationTest
{
using Microsoft.TeamFoundation.VersionControl.Client;
class Program
{
static void Main()
{
string workspaceLocalPath = #"C:\Dev";
var info = Workstation.Current
.GetLocalWorkspaceInfo(workspaceLocalPath);
// info is always null here
}
}
}
What I have already checked:
The exact same code works on my other machine the way it should.
I have verified that I have a workspace at C:\Dev
I have created a new workspace and in a different directory and changed the workspaceLocalPath variable in the code to match.
I have consulted the documentation which states that the return value will be null if the path is not in a workspace. From the above image, the path should be in a workspace.
Yet, everything seems to suggest this should work. Is there anything I could be missing?
After migrating from TFS2013 to TFS2017 in the company I work for I had the same problem with Workstation.Current.GetLocalWorkspaceInfo.
What worked for me is a call to Workstation.EnsureUpdateWorkspaceInfoCache:
TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("<your-tfs-uri-here>"));
VersionControlServer tfServer = tpc.GetService<VersionControlServer>();
Workstation.Current.EnsureUpdateWorkspaceInfoCache(tfServer, tfServer.AuthorizedUser);
I added the above code lines to the constructor of my TFS proxy class that uses GetLocalWorkspaceInfo.
When executing tf workspaces (on my computer) in the Visual Studio 2010 command prompt it says No workspace matching * found on this computer, but when executing the same command in Visual Studio 2012 it returns back all my expected workspaces.
The issue can be resolved by doing any of the following:
Reference the version of the Microsoft.TeamFoundation.VersionControl.Client dll that was connected with Visual Studio 2012 instead of the dll connected with Visual Studio 2010.
Open Visual Studio 2010 and connect it to TFS to where it will create the workspaces for Visual Studio 2010
I know this is an old post, but just like to share the workaround that we have, by using VersionControlServer.QueryWorkspaces to query all the workspaces for the user on his/her machine.
private static Workspace FindWorkspaceByPath(TfsTeamProjectCollection tfs, string workspacePath)
{
VersionControlServer versionControl = tfs.GetService<VersionControlServer>();
WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(workspacePath);
if (workspaceInfo != null)
{
return versionControl.GetWorkspace(workspaceInfo);
}
// No Workspace found using method 1, try to query all workspaces the user has on this machine.
Workspace[] workspaces = versionControl.QueryWorkspaces(null, Environment.UserName, Environment.MachineName);
foreach (Workspace w in workspaces)
{
foreach (WorkingFolder f in w.Folders)
{
if (f.LocalItem.Equals(workspacePath))
{
return w;
}
}
}
throw new Exception(String.Format("TFS Workspace cannot be determined for {0}.", workspacePath));
}
In my case, this issue occurred because of VersionControl.config file put under TFS cache folder (C:\Users\DeepakR\AppData\Local\Microsoft\Team Foundation\5.0\Cache\Volatile\0cb76a25-2556-4bd6-adaa-5e755ac07355_http) goes for a toss i.e. the configured workspace information weren't available as expected.
So, it basically needs a refresh of VersionControl.config file. Auto Refresh happens when Visual Studio gets loaded again i.e. it pulls the configured workspace information from Server and updates the config file or even if we execute tf command utility (tf.exe workspaces /collection:TFSURL)
Microsoft.TeamFoundation.VersionControl.Client's (v12.0.0.0) Workstation class has a function EnsureUpdateWorkspaceInfoCache which will do the same trick
VersionControlServer vcs = (VersionControlServer)tpc.GetService(typeof(VersionControlServer));
Workstation.Current.EnsureUpdateWorkspaceInfoCache(vcs, Environment.UserName);
https://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.workstation.ensureupdateworkspaceinfocache(v=vs.120).aspx
Hope the suggestion helps to resolve the issue.
I had this issue recently (today) using Visual Studio 2017, plus several other versions installed and a number of local workspaces.
I ended up updating the 'Team Foundation Server Client' NuGet package to the latest version (15.x) through the 'Manage NuGet Packages' menu and that fixed it.
I did also remove the existing project references first but that part might depend on what you need.
Simply run with the tricks.
Nothing is going to work properly without a proper DLL reference. The below had fixed the same issue i had for 5 days as it was screwing my time up.
Place the below DLL's in the bin folder of your project and give a reference to the whole solution for all the DLL's. If any error comes up like 'Reference could not be given' ignore it and skip that DLL from giving reference instead just place also the error creating DLL in bin folder which the project will automatically take during build
DLL's:
Microsoft.TeamFoundation.Client.dll
Microsoft.TeamFoundation.Common.dll
Microsoft.TeamFoundation.Core.WebApi.dll
Microsoft.TeamFoundation.TestManagement.Client.dll
Microsoft.TeamFoundation.TestManagement.Common.dll
Microsoft.TeamFoundation.Work.WebApi.dll
Microsoft.TeamFoundation.WorkItemTracking.Client.DataStoreLoader.dll
Microsoft.TeamFoundation.WorkItemTracking.Client.dll
Microsoft.TeamFoundation.WorkItemTracking.Common.dll
Microsoft.TeamFoundation.WorkItemTracking.Controls.dll
Microsoft.TeamFoundation.WorkItemTracking.Proxy.dll
Microsoft.TeamFoundation.WorkItemTracking.WebApi.dll
Microsoft.VisualStudio.Services.Client.Interactive.dll
Microsoft.VisualStudio.Services.Common.dll
Microsoft.VisualStudio.Services.WebApi.dll
Microsoft.WITDataStore32.dll
Microsoft.WITDataStore64.dll
The above dll's can be found in the below path if the System is installed with MTM or TFS
Path: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer
In my C:\Users\<username>\AppData\Local\Microsoft\Team Foundation folder I had 2 folders:
7.0
8.0
Within the 8.0 folder was the following folder:
\Cache\Volatile\c1dbda02-c575-4dd2-b221-e83f7cb63665_http
But within the 7.0 folder the \Cache\Volatile folder was empty
So all I did was copy across the c1dbda02-c575-4dd2-b221-e83f7cb63665_http folder into 7.0\Cache\Volatile\
After this GetLocalWorkspaceInfo call returned the workspace info successfully
This is how to find workspace when you have server path:
Workspace[] workspaces = _versionControl.QueryWorkspaces(null, Environment.UserName, Environment.MachineName);
return workspaces.FirstOrDefault(w => !string.IsNullOrEmpty(w.TryGetLocalItemForServerItem(ConstDefaultFlowsTfsPath)));
Where ConstDefaultFlowsTfsPath is server path with "$" for instance : "$/MyCompany/Services/DiagnosticsFlows"
You could also replace the last line to:
return workspaces.FirstOrDefault(w => !string.IsNullOrEmpty(w.GetServerItemForLocalItem(myLocalPath)));
and that should work for you too.

UnauthorizedAccessException when downloading a file using the TFS SDK

When I try to download a file from TFS version control SDK to my computer I receive an 'UnauthorizedAccessException' saying Access to the local path I'm trying to download to is denied. I included a stripped down version of the code I am using below.
var projectCollection = GetProjectCollection();
var versionControl = (VersionControlServer)projectCollection.GetService(typeof(VersionControlServer));
versionControl.DownloadFile('$/path to file', 'local path to download to');
Does anyone know how to resolve this issue?
I found the issue.
The second argument in DownloadFile() needs to be the file name it will be downloaded as and not the parent directory it will be placed in. I thought it just needed the directory name.
So instead of what I originally had
versionControl.DownloadFile("$/Readme.txt", "C:\\Temp");
it needs to be
versionControl.DownloadFile("$/Readme.txt", "C:\\Temp\\Readme.txt");
This is because the process does not have rights to the local path. Make sure the local path has the appropriate right set to the user that is running the process.

Categories