2017-06-30 131 views
0

我需要通過OAuth2連接WebService中的VS-Team-Services。
我在官方網站上發現了sample solution,但我無法修改它在我們的Web服務代碼中使用它。
因爲它在請求訪問令牌時請求許可Accept or Deny
我需要它自動授予訪問沒有權限頁的令牌。通過Asp.Net WebForms訪問VS-Team Services OAuth

這是我以前的工作代碼沒有OAuth。我需要根據OAuth修改此代碼。

private Dictionary<string, MyTfsTeamProjectCollectionHelper> GetTFSProjectCollections() 
{ 
    try 
    { 
     TFSConnectionProviderHelper connect = new TFSConnectionProviderHelper(); 
     ICredentials iCred = new NetworkCredential(strTFSUsername, strTFSPassword); 
     connect.GetCredentials(new Uri(ConfigurationManager.AppSettings["ServerURL"]), iCred); 
     TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(ServerURL), connect); 
     configurationServer.EnsureAuthenticated(); 

     ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None); 
     DictionaryTfsTeamProjectCollection = new Dictionary<string, MyTfsTeamProjectCollectionHelper>(); 
     foreach (CatalogNode tpcNode in collectionNodes) 
     { 
      MyTfsTeamProjectCollectionHelper myTfsTeamProjectCollection = new MyTfsTeamProjectCollectionHelper(); 

      Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]); 
      TfsTeamProjectCollection tfsTeamProjectCollection = configurationServer.GetTeamProjectCollection(tpcId); 
      myTfsTeamProjectCollection.TfsTeamProjectCollection = tfsTeamProjectCollection; 

      DictionaryTfsTeamProjectCollection.Add(tfsTeamProjectCollection.Name.Replace(tfsTeamProjectCollection.ConfigurationServer.Name.ToString() + "/", "").ToLower(), myTfsTeamProjectCollection); 
     } 
     return DictionaryTfsTeamProjectCollection; 
    } 
    catch (Exception ex) 
    { 
     Logger.WriteToLog(Logger.LogType.Error, strFlow, strCaseno, strTFSID, strUser, ex.Message); 
     Logger.WriteToLog(Logger.LogType.Error, strFlow, strCaseno, strTFSID, strUser, " StackTrace: " + ex.StackTrace); 
     return null; 
    } 


} 
private Project GetTFSProject(string tfsProjectCollectionName, string StrTFSProject) 
{ 
    try 
    { 
     GetTFSProjectCollections(); 
     Project project = null; 
     if (DictionaryTfsTeamProjectCollection != null && DictionaryTfsTeamProjectCollection.Keys.Contains(tfsProjectCollectionName.ToLower())) 
     { 
      TfsTeamProjectCollection myTeamProjectCollection = DictionaryTfsTeamProjectCollection[tfsProjectCollectionName.ToLower()].TfsTeamProjectCollection; 
      WorkItemStore workItemStore = myTeamProjectCollection.GetService<WorkItemStore>(); 
      project = (from Project pr in workItemStore.Projects select pr).ToList().Where(i => i.Name.ToLower() == StrTFSProject.ToLower()).SingleOrDefault(); 
     } 
     else 
     { 
      Logger.WriteToLog(Logger.LogType.Error, strFlow, strCaseno, strTFSID, StrTFSProjectcollection, "ProjectCollectionName does not exist:" + tfsProjectCollectionName); 
     } 
     return project; 
    } 
    catch (Exception ex) 
    { 
     Logger.WriteToLog(Logger.LogType.Error, strFlow, strCaseno, strTFSID, strUser, ex.Message); 
     Logger.WriteToLog(Logger.LogType.Error, strFlow, strCaseno, strTFSID, strUser, " StackTrace: " + ex.StackTrace); 
     return null; 
    } 
} 

權限頁面的屏幕截圖:

enter image description here

我們是一個WebForms模板。是否可以通過簡單的Asp.Net Webforms模板訪問VSTS OAuth模板?

回答

0

如果沒有權限授權頁面,用戶必須手動接受或拒絕它,否則無法進行此操作。但是,如果訪問令牌或刷新令牌(1年到期)有效,則不需要再次授權。

不存在具有VSTS OAuth模板的Asp.Net Webforms,但是不管使用哪種類型的Web應用程序,工作流都是相同的。

+0

**有VSTS OAuth模板的Asp.Net Webforms ** - 是否有樣本?如果是的話,請你指出正確的方向。 –

+0

對不起,我的意思是沒有.... –