使用Windows服务使用TFS令牌连接到TFS服务器时出错

问题描述 投票:0回答:1

这里是场景。我编写了一个Windows服务,该服务连接到本地TFS服务器上的项目。我创建了一个TFS令牌,并正在使用它来登录项目,以便可以向该项目中添加项目。我不断收到错误TF30063:您无权访问https:// 。*** / DefaultCollection

任何人都知道我在这方面缺少什么吗?

public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
    {            
        try
        {
            using (ExternalApiService externalApiService = new ExternalApiService
            {
                Credentials = new NetworkCredential { UserName = "*********", Password = "*******" }
            })
            {
                SqlConnection conn = new SqlConnection();


                conn.ConnectionString = "Data Source=TFS; Initial Catalog=Tfs_DefaultCollection; Trusted_Connection=true";
                conn.Open();

                externalApiService.Url = "*****************";

                Search[] searches = externalApiService.listSearches(new ListSearchesRequest());

                long allOpenItemsSearchId = (from s in searches
                                             where s._searchName == "UJATFS items"
                                             select s._searchId).First();

                SearchRow[] allOpenItems = externalApiService.runSearch(new RunSearchRequest { _searchId = allOpenItemsSearchId });

                foreach (SearchRow searchRow in allOpenItems)
                {
                    int descriptionIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Short Description");
                    int ticketNumberIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Ticket Number");
                    int typeIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Type");
                    int subCategoryIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Subcategory");
                    int categoryIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Category");
                    int statusDetailCodeIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Status Detail Code");
                    int technicianNotesIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Technician Notes");
                    int tfsCrossReferenceIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "TFS Cross Reference");
                    int ticketCrossReferenceIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Ticket Cross Reference");
                    int priorityIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Priority");
                    int locationIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Location");
                    int fNameIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "First Name");
                    int lNameIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Last Name");
                    int phoneIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Phone Number");
                    int jobTitleIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Job Title");
                    int createdDateIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Created On");
                    int notesIndex = Array.FindIndex(searchRow._itemFields, w => w.fieldName == "Notes");

                    TeamFoundation tf = new TeamFoundation();

                    if (descriptionIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[descriptionIndex]).fieldValue.Length > 0)
                        {
                            tf.Description = ((itemField)searchRow._itemFields[descriptionIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (ticketNumberIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[ticketNumberIndex]).fieldValue.Length > 0)
                        {
                            tf.TicketNumber = ((itemField)searchRow._itemFields[ticketNumberIndex]).fieldValue[0].ToString();
                        }
                    } 

                    if (typeIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[typeIndex]).fieldValue.Length > 0)
                        {
                            tf.Type = ((itemField)searchRow._itemFields[typeIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (subCategoryIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[subCategoryIndex]).fieldValue.Length > 0)
                        {
                            tf.SubCategory = ((itemField)searchRow._itemFields[subCategoryIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (categoryIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[categoryIndex]).fieldValue.Length > 0)
                        {
                            tf.Category = ((itemField)searchRow._itemFields[categoryIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (statusDetailCodeIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[statusDetailCodeIndex]).fieldValue.Length > 0)
                        {
                            tf.DetailStatus = ((itemField)searchRow._itemFields[statusDetailCodeIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (technicianNotesIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[technicianNotesIndex]).fieldValue.Length > 0)
                        {
                            tf.TechnicianNotes = ((itemField)searchRow._itemFields[technicianNotesIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (priorityIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[priorityIndex]).fieldValue.Length > 0)
                        {
                            tf.Priority = ((itemField)searchRow._itemFields[priorityIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (ticketCrossReferenceIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[ticketCrossReferenceIndex]).fieldValue.Length > 0)
                        {
                            tf.TicketCrossReferenceNum = ((itemField)searchRow._itemFields[ticketCrossReferenceIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (locationIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[locationIndex]).fieldValue.Length > 0)
                        {
                            tf.Location = ((itemField)searchRow._itemFields[locationIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (fNameIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[fNameIndex]).fieldValue.Length > 0)
                        {
                            tf.FirstName = ((itemField)searchRow._itemFields[fNameIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (lNameIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[lNameIndex]).fieldValue.Length > 0)
                        {
                            tf.LastName = ((itemField)searchRow._itemFields[lNameIndex]).fieldValue[0].ToString();
                        }
                    }

                    tf.Requestor = tf.FirstName + " " + tf.LastName;

                    if (phoneIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[phoneIndex]).fieldValue.Length > 0)
                        {
                            tf.PhoneNum = ((itemField)searchRow._itemFields[phoneIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (jobTitleIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[jobTitleIndex]).fieldValue.Length > 0)
                        {
                            tf.JobTitle = ((itemField)searchRow._itemFields[jobTitleIndex]).fieldValue[0].ToString();
                        }
                    }

                    if (createdDateIndex >= 0)
                    {
                        if (((itemField)searchRow._itemFields[createdDateIndex]).fieldValue.Length > 0)
                        {
                            tf.CreateDate = ((itemField)searchRow._itemFields[createdDateIndex]).fieldValue[0].ToString();
                        }
                    }

                    long itemID = 0;
                    GetItemDetailsRequest getItem = new GetItemDetailsRequest();

                    itemID = searchRow._itemId;

                    getItem._itemId = itemID;
                    getItem._itemDefinitionId = 10257;

                    TicketDetailsResponse detailsResponse = externalApiService.getTicketDetails(getItem);

                    DescriptionDetail[] detail = detailsResponse._allDescriptionsList;

                    int wo_num = 0;
                    string task = "";
                    string type = "";
                    string workOrderCrossReference = "";

                    Uri tfsUri = new Uri("https://tfs.courtswv.gov/DefaultCollection");
                    TfsConfigurationServer configurationServer =
                            TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);

                    VssBasicCredential vssBasicCredential = new VssBasicCredential("", ConfigurationManager.AppSettings["TFSToken"]);

                    TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(tfsUri, vssBasicCredential);
                    teamProjectCollection.EnsureAuthenticated();

                    WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();


                    string projectName = "UJA";

                    Project teamProject = workItemStore.Projects[projectName];
                    WorkItemType workItemType = teamProject.WorkItemTypes["Service Desk Ticket"];

                    wo_num = tf.WONum;
                    type = tf.Type;
                    task = tf.Description;
                    workOrderCrossReference = tf.TicketCrossReferenceNum;

                    WorkItem userStory = new WorkItem(workItemType)
                    {
                        Title = task,
                        Description = "Requestor: " + tf.Requestor + ":  " + task
                    };

                    userStory.History = tf.TechnicianNotes;
                    userStory.Fields["Work Order Cross Reference"].Value = tf.TicketCrossReferenceNum;
                    userStory.Fields["Ticket#"].Value = tf.TicketNumber;
                    userStory.Fields["TrackIT Requestor"].Value = tf.Requestor;
                    userStory.Fields["TicketCreatedDate"].Value = tf.CreateDate;
                    userStory.Fields["TrackIT Location"].Value = tf.Location;
                    userStory.Fields["TrackIT Phone#"].Value = tf.PhoneNum;
                    userStory.Fields["TrackIT Job Title"].Value = tf.JobTitle;

                    //Get Status info for each work order number
                    //*******************************************************************
                    SqlCommand cmd = new SqlCommand("dbo.GetFtTFSStatus", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter parm = new SqlParameter("p_status_detail", SqlDbType.VarChar);
                    parm.Value = tf.DetailStatus;

                    cmd.Parameters.Add(parm);

                    DataTable dt = new DataTable();

                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    da.Dispose();
                    //********************************************************************
                    //userStory.Fields["State"].Value = "Proposed";
                    //userStory.Save();

                    //userStory.Fields["State"].Value = "Active";
                    //userStory.Save();

                    if (dt.Rows.Count > 0)
                    {
                        userStory.Fields["Area Path"].Value = projectName + "\\" + dt.Rows[0]["TFSAREA"].ToString();
                        userStory.Fields["State"].Value = dt.Rows[0]["TFSSTATE"].ToString();
                        userStory.Fields["Triage"].Value = dt.Rows[0]["TFSTRIAGE"].ToString();
                    }

                    //Get Priority info for each work order number
                    //*******************************************************************
                    SqlCommand cmd2 = new SqlCommand("dbo.GetFtTFSPriority", conn);
                    cmd2.CommandType = CommandType.StoredProcedure;
                    SqlParameter parm2 = new SqlParameter("p_priority", SqlDbType.VarChar);
                    parm2.Value = tf.Priority;

                    cmd2.Parameters.Add(parm2);

                    DataTable dt2 = new DataTable();

                    SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
                    da2.Fill(dt2);
                    da2.Dispose();
                    //********************************************************************

                    if (dt2.Rows.Count > 0)
                    {
                        userStory.Fields["Priority"].Value = dt2.Rows[0]["TfsPriority"].ToString();
                    }

                    //Get assigned user
                    //*******************************************************************
                    SqlCommand cmd3 = new SqlCommand("Custom.spGetUserName", conn);
                    cmd3.CommandType = CommandType.StoredProcedure;

                    DataTable dt3 = new DataTable();

                    SqlDataAdapter da3 = new SqlDataAdapter(cmd3);
                    da3.Fill(dt3);
                    da3.Dispose();
                    //********************************************************************

                    userStory.Fields["Assigned To"].Value = dt3.Rows[0]["TfsUserName"].ToString();

                    foreach (DescriptionDetail desc in detail)
                    {
                        userStory.History = desc._data;
                        userStory.Save();
                    }

                    userStory.Save();

                    LogService("Ticket#: " + userStory.Id.ToString());

                    //Put the TFS cross reference number into Footprints                        
                    long returnItem = 0;
                    bool returnBool = false;
                    string[] tfsData = new string[1];
                    tfsData[0] = userStory.Id.ToString();
                    string[] tfsDetailCode = new string[1];
                    tfsDetailCode[0] = "Closed";

                    EditTicketRequest editTicketRequest = new EditTicketRequest();

                    itemField[] items = new itemField[2];
                    items[0] = new itemField();
                    items[0].fieldName = "TFS Cross Reference";
                    items[0].fieldValue = tfsData;

                    items[1] = new itemField();
                    items[1].fieldName = "Status Detail Code";
                    items[1].fieldValue = tfsDetailCode;

                    editTicketRequest._ticketFields = items;

                    editTicketRequest._ticketId = itemID;

                    editTicketRequest._ticketDefinitionId = 10257;

                    try
                    {
                        externalApiService.editTicket(editTicketRequest, out returnItem, out returnBool);
                    }
                    catch (Exception e)
                    {
                        LogService("Error: " + e.Message);
                        continue;
                    }

                }
            }
        }
        catch (Exception e)
        {
            LogService(e.Message.ToString());
            return;
        }
    } 
c# service tfs
1个回答
0
投票

这对我有用:

Uri tfsUri = new Uri("https://tfs.courtswv.gov/DefaultCollection");
var networkCredential = new NetworkCredential(String.Empty, ConfigurationManager.AppSettings["TFSToken"]);
VssBasicCredential vssBasicCredential = new VssBasicCredential(networkCredential);
VssCredentials tfsCredentials = new VssCredentials(vssBasicCredential);
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(tfsUri), tfsCredential)
tpc.Authenticate();

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.