2017-09-26 2 views
1

こんにちはすべて私はログインコントローラからユーザデータストアにいくつかのデータを保存しようとしています。アクティビティなしで登録されていないlocalhostボットの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); 
} 

私azuerアプリケーションポータルでregested /私はあなたがボットの状態にアクセスするためのAppIDにappPasswordを使用することができる場所の例を見てきましたが、あなたのボットが公開されるまで、私の理解するものは使用できません。現在できません。

また、アクセス権のないアクティビティからアクセスすることもできます。

これは実際に私の計画は最終的にAzureテーブルストレージにユーザーデータを保存することですが、私は一時的な解決策を提示したいと思います。私は、ローカルテキストファイルに辞書をシリアライズしてデシリアライズすることを検討していますが、これは残念なことです。

すべてのサポートに感謝します。このラインでは

答えて

0

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

あなたはhttp://localhost:3979/で状態サービスを使用するようにBotBuilderに指示されているが、何の状態サービスは、そのエンドポイントではありません。

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テーブルストレージを追加するまで、一時的な解決策を持っているしたい場合は

は、あなたがInMemoryDataStoreを使用することができ、これはInMemoryDataStoreたらhttps://www.nuget.org/packages/Microsoft.Bot.Builder.Azure/

をパッケージnuget Azureの拡張機能を必要とし登録されている場合は、次のような方法でアクセスできます:

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); 
} 
関連する問題