Getting error while Authenticating online TFS from WCF service - c#

I am trying to connect online TFS using WCF service but it is throwing me exception "TF30063: You are not authorized to access https://abdul-r.visualstudio.com/DefaultCollection/TestTFS.".
Below is my sample code
NetworkCredential netCred = new NetworkCredential(
"*MyEmail*",
"*MyPassword*");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials credential = new TfsClientCredentials(basicCred);
credential.AllowInteractive = false;
string TFSServerPath = "https://abdul-r.visualstudio.com/DefaultCollection/TestTFS";
using (TfsTeamProjectCollection tfs1 = new TfsTeamProjectCollection(new Uri(TFSServerPath), credential))
{
tfs1.EnsureAuthenticated();
}
Any help would be appreciated.

Related

Error 403 when trying to read emails using EWS

I'm receiving error 403 when trying to read e-mails using code below
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithTenantId(tenant)
.WithAuthority(new Uri(String.Format(CultureInfo.InvariantCulture, instance, tenant)))
.WithRedirectUri(redirectUri)
.Build();
string[] scopes = new string[] { "https://outlook.office365.com/.default" };
result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
if (result != null)
{
string token = result.AccessToken;
service = new ExchangeService
{
Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"),
Credentials = new OAuthCredentials(token),
ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, email)
};
service.HttpHeaders.Add("X-AnchorMailbox", email);
}
var emails = service.FindItems(WellKnownFolderName.Inbox, new FolderView(10));
On Azure portal I did:
Register new application and copied tenant id, client id, client secret
Added EWS.AccessAsUser.All in API auth.
But still receiving 403 - Forbidden when trying to get email from inbox
What am I missing?

Basic authentication requires a secure connection to the server. in TFS

I was try to connect my TFS server using my credentials . But i am getting error 'Basic authentication requires a secure connection to the server.'
string username = "adminuser";
string pwd = "mypassword";
string domain = "http://localhost:8080/tfs/defaultcollection";
NetworkCredential networkCredential = new NetworkCredential(username, pwd);
BasicAuthCredential basicAuthCredential = new BasicAuthCredential(networkCredential);
TfsClientCredentials tfsClientCredentials = new TfsClientCredentials(basicAuthCredential)
{
AllowInteractive = false
};
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(domain), tfsClientCredentials);
tfs.EnsureAuthenticated();
My tfs didn't have the https. Any alternative to fix it But browser level it is working fine
The BasicAuthCredential requires https://, I believe, and I wasn't able to access my TFS with https://. So I found another way to get from NetworkCredential to VssCredentials.
string username = "adminuser";
string pwd = "mypassword";
string domain = "http://localhost:8080/tfs/defaultcollection";
NetworkCredential networkCredential = new NetworkCredential(username, pwd);
//BasicAuthCredential basicAuthCredential = new BasicAuthCredential(networkCredential);
Microsoft.VisualStudio.Services.Common.WindowsCredential winCred = new Microsoft.VisualStudio.Services.Common.WindowsCredential(networkCredential);
VssCredentials vssCred = new VssClientCredentials(winCred);
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(domain), vssCred);
tfs.EnsureAuthenticated();
Try using the following code:
String collectionUri = "http://localhost:8080/tfs/defaultcollection";
VssCredentials creds = new VssClientCredentials();
creds.Storage = new VssClientCredentialStorage();
VssConnection connection = new VssConnection(new Uri(collectionUri), creds);

getting error like "Connection did not succeed. Try again later". using Microsoft.Exchange with delegates and Impersonation

below Useexchangeservice method used for connect mail server and getting mail from server but its showing error like "Connection did not succeed. Try again later".
static void UseExchangeService()
{
string userEmailAddress = "xyz2#domain.com";
string userPassword = "testnew#123";
ExchangeService service = new
ExchangeService(ExchangeVersion.Exchange2010_SP2);
#region Authentication
service.UseDefaultCredentials = true;
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
var folder = new FolderId(WellKnownFolderName.Calendar, new Mailbox(userEmailAddress));
#endregion
#region Endpoint management
service.AutodiscoverUrl(userEmailAddress,
RedirectionUrlValidationCallback);
#endregion
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("Lalit.Sharma2#securemeters.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("Sent by using the EWS Managed API");
email.Save(new FolderId(WellKnownFolderName.Drafts, "Lalit.Sharma2#securemeters.com"));
email.Send();
}

The specified folder could not be found in the store Error

I'm trying to retrieve the list of appointment from a specific meeting room using Exchange Web Services managed API.
I'm using Office365 and Exchange Online.
I tried the following code.
try
{
ExchangeService newExchangeService = new ExchangeService (ExchangeVersion.Exchange2013);
//Admin permission account
newExchangeService.Credentials = new NetworkCredential(username, password);
newExchangeService.AutodiscoverUrl(email-id, RedirectionUrlValidationCallback);
SearchFilter.SearchFilterCollection searchFilter = new SearchFilter.SearchFilterCollection();
searchFilter.Add(new SearchFilter.IsGreaterThanOrEqualTo(AppointmentSchema.Start, startDate));
searchFilter.Add(new SearchFilter.IsLessThanOrEqualTo(AppointmentSchema.Start, endDate));
ItemView view = new ItemView(50);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.AppointmentType, AppointmentSchema.End);
var calendarSearch = new FolderId(WellKnownFolderName.Calendar, new Mailbox("adachi#fairuse.jp"));
var appointmentresult = service.FindItems(calendarSearch, searchFilter, view);
}catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
I got the error:
The specified folder could not be found in the store.
Apparently this is a permissions issue, but where is this permission set?
You don't have to create a new mailbox, a string with only the mail address is enough:
var calendarSearch = new FolderId(WellKnownFolderName.Calendar, "adachi#fairuse.jp");
As for permissions: Ar you in a domain? Do you use NTLM or ADFS?

Error authorized to tfs programmatically

I have local tfs 2012. On Visual studio 2012 , use c# , I write program which programmatically connect to tfs server. But I have error:
{"TF30063: You are not authorized to access http://server:8080/tfs."} System.Exception {Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException}
My code:
Uri collectionUri = new Uri("http://server:8080/tfs");
NetworkCredential netCred = new NetworkCredential("login","password");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;
TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collectionUri, netCred);
teamProjectCollection.EnsureAuthenticated();//throw error here
Can you help me fix this error?
P.S. I try do connect this way, but I have same error:
var projectCollection = new TfsTeamProjectCollection(
new Uri("http://myserver:8080/tfs/DefaultCollection"),
new NetworkCredential("youruser", "yourpassword"));
projectCollection.Connect(ConnectOptions.IncludeServices);
And this way:
Uri collectionUri = new Uri("http://server:8080/tfs/DefaultCollection");
NetworkCredential netCred = new NetworkCredential("login","password","Server.local");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;
TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collectionUri, netCred);
teamProjectCollection.EnsureAuthenticated();
Hope it helps you.
var tfsCon = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://server:8080/tfs"));
tfsCon.Authenticate();
var workItems = new WorkItemStore(tfsCon);
var projectsList = (from Project p in workItems.Projects select p.Name).ToList();
or
Uri TfsURL = new Uri(""http://server:8080/tfs"");
NetworkCredential credential = new NetworkCredential(Username, Password, Domain);
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(TfsURL, credential);
collection.EnsureAuthenticated();
If fails here you need to configure in App.config to set the default proxy:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
</system.net>
</configuration>

Categories

Resources