2016-05-17 35 views
2

更新1整個登錄控制器現在位於片段中。GetUserDataAsync從連接器客戶端拋出「訪問被拒絕」錯誤

更新2它現在固定。問題在於,我使用模擬器運行bot,連接器客戶端不喜歡它。當bot託管並且此用戶標識與模擬器生成的用戶標識不匹配時,ConnectorClient生成用戶標識。因此訪問被拒絕。長話短說bot必須被託管,並且應該使用connectorClient的有效通道之一來訪問以接受您的請求。希望它可以幫助別人。

更新3在API路線修正了小錯誤@Vish

我一直在試圖建立一個OAuth流的應用程序。我設法從服務中獲取userInfo,但無法將訪問令牌與bot中的用戶標識綁定。

public class LoginController : ApiController 
{ 
    [HttpGet, Route("api/login")] 
    public RedirectResult Login(string userid) 
    { 
     return Redirect(String.Format("https://login.windows.net/microsoft.com/oauth2/authorize?response_type=code&client_id={0}&redirect_uri={1}&resource={2}&state={3}", 
      "MyClientId", HttpUtility.UrlEncode(Constants.apiBasePath + userid + "/authorize"),HttpUtility.UrlEncode('MyService'), userid)); 
    } 

    [HttpGet, Route("api/authorize")] 
    public async System.Threading.Tasks.Task<HttpResponseMessage> Authorize(string state, string code) 
    { 
     string userid = state; 
     AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/microsoft.com/oauth2/authorize/"); 
     ClientCredential cc = new ClientCredential("MyClientId", "MyClientSecret"); 
     AuthenticationResult ar = await ac.AcquireTokenByAuthorizationCodeAsync(code, new Uri(Constants.apiBasePath + userid + "/authorize"), cc); 
     if (!String.IsNullOrEmpty(ar.AccessToken)) 
     { 
      var client = new ConnectorClient(Constants.botId,Constants.botSecret); 
      var getData = await client.Bots.GetUserDataAsync(Constants.botId, userid); 
      getData.Data = ar.Serialize(); 

      var foo = await client.Bots.SetUserDataAsync(Constants.botId, userid, getData); 

      //return Request.CreateResponse(foo); 
      var response = Request.CreateResponse(HttpStatusCode.Moved); 
      response.Headers.Location = new Uri("/loggedin.htm", UriKind.Relative); 
      return response; 


     } 
     else 
      return Request.CreateResponse(HttpStatusCode.Unauthorized); 

    } 

在上面的驗證結果'ar'上的代碼片段成功地生成了所有需要的數據。現在在以下幾行中,我無法從GetUserDataAsync函數獲取userData對象。它總是拋出以下錯誤。

{ 
    "message": "An error has occurred.", 
    "exceptionMessage": "Access Denied", 
    "exceptionType": "Microsoft.Rest.HttpOperationException", 
    "stackTrace": " at Microsoft.Bot.Connector.ErrorHandling.HandleError[ObjectT] 
} 
+0

向我們展示LoginController代碼?確保您的登錄操作沒有授權屬性或類似的東西。 – CodeNotFound

+0

@CodeNotFound我剛剛更新了整個LoginController的問題。我確實從登錄服務獲取了令牌。 'ar'是一個完全有效的數據對象,具有我需要的全部信息。 我想我無法使用botConnector訪問botFramework數據。 –

回答

1

我認爲你需要遵循FacebookAuthSample中顯示的ResumptionCookie方法。在該示例中,您將看到令牌存儲在對話框中,而不是存儲在Web API中。

此外,您還可以嘗試通過以下方式

using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message)) 
{ 
    var client = scope.Resolve<IConnectorClient>(); 
    client.Messages.SendMessage(message); 
} 
+0

謝謝,我會檢查恢復Cookie方法。 但是這種方法也應該起作用。這個想法是將認證詳細信息保存在botUserData中,這將在整個會話中持續存在。 爲此,我需要使用GetUserDataAsync方法無法獲取的botUserData。 我試圖模仿這[這裏](http://docs.botframework.com/connector/tracking-bot-state/#concurrency)。方法名稱是不同的,我不知道爲什麼,visual studio建議我用作GetUserData的方法不是一個有效的方法。 –

+0

使用恢復Cookie方法,您將能夠將身份驗證詳細信息保存在bot用戶數據中。 –

1

更新實例化客戶端: 現在這是固定的。問題是我在Azure Portal中配置了身份驗證,這在本文中不是必需的。這導致了額外一層所需的身份驗證,阻止用戶訪問像Slack等通道上的Bot。當我在Azure門戶中刪除身份驗證時,bot開始工作,並且我可以通過代碼使用Azure AD進行身份驗證。

我無法添加評論(需要50代表),所以在此發佈我的問題。將在一段時間內刪除。

@ManikantaReddy,我遵循相同的示例,但一直面臨不同的問題得到這個工作。嘗試點擊鏈接以獲得授權時,我總是收到401 Unauthorized。我的MessagesController.cs看起來像這樣:

 try 
     { 
      string d = (string)message.BotUserData; 
      AuthenticationResult ar = AuthenticationResult.Deserialize(d); 
      AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize/"); 
      ar = DateTimeOffset.Compare(DateTimeOffset.Now, ar.ExpiresOn) < 0 ? ar : await ac.AcquireTokenByRefreshTokenAsync(ar.RefreshToken, new ClientCredential(Constants.ADClientId, Constants.ADClientSecret)); 
     } 
     catch (Exception ex) 
     { 
      return message.CreateReplyMessage($"You must authenticate to use bot: https://mybot.azurewebsites.net/api/{message.From.Id}/login"); 
     } 

我的loginController與您的看起來完全一樣。我錯過了什麼嗎?

+0

是!我也有這個問題。我應該在這裏修改代碼。所以基本上發生了什麼是您的身份驗證網址未找到。而不是在請求路徑中具有用戶標識,請將其作爲參數發送。 在回覆信息中使用 '$「您必須先進行身份驗證才能使用bot:https://mybot.azurewebsites.net/api/login?userid={message.From.Id}」' 然後更改url api爲 'api/login'而不是'api/{userid}/login'和 'api/authorize'代替'api/{userid}/authorize' –

+0

哦,因爲我們不能訪問到userid,我們將它作爲'state'在授權url中傳遞。然後,微軟會在我們的url處調用回來,而不會改變'state'的值。因此,我們可以檢索我們的用戶名並授權。請參閱上面更新的代碼以獲得實施。 –

+0

@ManikantaReddy,謝謝你的詳細回覆! – Vish

相關問題