2017-09-26 59 views
1

大家好,我試圖將一些數據從登錄控制器保存到用戶數據存儲。通過StateClient訪問用戶數據的本地主機機器人沒有註冊沒有活動

[HttpGet, Route("api/{channelId}/{userId}/authorize")] 
public async System.Threading.Tasks.Task<HttpResponseMessage> Authorize(string channelId, string userId, string code) 
{ 
    string protocalAndDomain = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); 

    AuthenticationContext ac = new AuthenticationContext(Constants.AD_AUTH_CONTEXT); 
    ClientCredential cc = new ClientCredential(Constants.AD_CLIENT_ID, Constants.AD_CLIENT_SECRET); 
    AuthenticationResult ar = await ac.AcquireTokenByAuthorizationCodeAsync(code, new Uri(protocalAndDomain + "/api/" + channelId + "/" + userId + "/authorize"), cc); 
    MicrosoftAppCredentials.TrustServiceUrl(protocalAndDomain, DateTime.Now.AddHours(1)); 

    if (!String.IsNullOrEmpty(ar.AccessToken)) 
    { 
     // Store access token & User Id to bot state 
     //var botCred = new MicrosoftAppCredentials(Constants.MS_APP_ID, Constants.MS_APP_PASSWORD); 
     //https://state.botframework.com 

     using (var sc = new StateClient(new Uri("http://localhost:3979/"))) 
      if (sc != null) 
      { 
       var botData = new BotData(data: null, eTag: "*"); 
       botData.SetProperty("accessToken", ar.AccessToken); 
       botData.SetProperty("userEmail", ar.UserInfo.DisplayableId); 

       //i get a 401 response here 
       await sc.BotState.SetUserDataAsync(channelId, userId, botData); 
      } 


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

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

我見過,你可以使用的AppId的appPassword訪問機器人狀態的例子,但我的理解,直到你的機器人發佈/ regested在azuer應用門戶那些不可用我目前無法做到。

或者您可以通過我無法訪問的活動訪問它。

這實際上只是一個臨時解決方案,我的計劃是最終將用戶數據保存到Azure表存儲中,但是我希望在此期間使用臨時解決方案;我正在考慮將字典序列化並反序列化爲本地文本文件,但是現在看起來似乎過度殺毒,似乎很愚蠢,我無法在本地保存到用戶數據而沒有在azure中註冊我的應用程序。

歡呼任何幫助,非常感謝。

回答

0

這一行:

var sc = new StateClient(new Uri("http://localhost:3979/")) 

你指示使用在http://localhost:3979/國家服務的BotBuilder但沒有國家服務的端點。

如果你想有一個臨時解決方案,直到您添加Azure的表存儲,您可以使用InMemoryDataStore

protected void Application_Start() 
{ 
    Conversation.UpdateContainer(
     builder => 
      { 
       builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly())); 

       var store = new InMemoryDataStore(); // volatile in-memory store 

       builder.Register(c => store) 
        .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore) 
        .AsSelf() 
        .SingleInstance(); 


      }); 

    GlobalConfiguration.Configure(WebApiConfig.Register); 
} 

注:這需要在Azure擴展NuGet包https://www.nuget.org/packages/Microsoft.Bot.Builder.Azure/

一旦InMemoryDataStore已註冊,您可以使用類似的東西訪問它:

var message = new Activity() 
       { 
        ChannelId = ChannelIds.Directline, 
        From = new ChannelAccount(userId, userName), 
        Recipient = new ChannelAccount(botId, botName), 
        Conversation = new ConversationAccount(id: conversationId), 
        ServiceUrl = serviceUrl 
       }.AsMessageActivity(); 

using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message)) 
{ 
    var botDataStore = scope.Resolve<IBotDataStore<BotData>>(); 
    var key = new AddressKey() 
    { 
     BotId = message.Recipient.Id, 
     ChannelId = message.ChannelId, 
     UserId = message.From.Id, 
     ConversationId = message.Conversation.Id, 
     ServiceUrl = message.ServiceUrl 
    }; 
    var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None); 

    userData.SetProperty("key 1", "value1"); 
    userData.SetProperty("key 2", "value2"); 

    await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None); 
    await botDataStore.FlushAsync(key, CancellationToken.None); 
} 
相關問題