我如何在LoginController使用服务url或微软应用凭证创建StateClient对象

本文关键字:应用 微软 凭证 创建 对象 StateClient url 服务 LoginController | 更新日期: 2023-09-27 18:06:57

我正在研究bot框架技术,在我的一个项目中,我想在我的LoginController.cs中使用服务Url或Microsoft凭据创建状态客户端对象

这是我在LoginController.cs中编写的代码,它在bot模拟器中工作,但在发布我的bot并将其添加到skype之后,它不工作。

 //[HttpGet, Route("api/{userid}/authorize")]
    [HttpGet, Route("api/{userid}/token")]
    public async System.Threading.Tasks.Task<HttpResponseMessage> Authorize(string userid, string code)
    {
        AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/common/oauth2/token/");
        ClientCredential cc = new ClientCredential(Constants.Constants.ADClientId, Constants.Constants.ADClientSecret);
        AuthenticationResult ar = await ac.AcquireTokenByAuthorizationCodeAsync(code, new Uri(Constants.Constants.apiBasePath + userid + "/token"), cc, "https://api.office.com/discovery/");
        if (!String.IsNullOrEmpty(ar.AccessToken))
        {
           // var stateClient = new StateClient(new Uri("http://localhost:9000/"));
            // var stateClient = new StateClient(new Uri("http://skype.botframework.com/"));
            //var microsoftAppCredentials = new MicrosoftAppCredentials(appId: "ace15a7e-b7f6-4776-a6a3-a58c0314bd01", password: "X7BS9hJt0skSBu6z9kgW1FB");
            //var stateClient = new StateClient(new Uri("http://skype.botframework.com/"),microsoftAppCredentials,addJwtTokenRefresher:true);
            //var stateClient = new StateClient(microsoftAppCredentials);
            var stateClient = new StateClient(new Uri("https://apis.skype.com/"));
            //var stateClient = new StateClient(new Uri("https://api.slack.com/"));
            if (stateClient != null)
            {
                var userId = userid.Replace("-", ":");
                var getData = await stateClient.BotState.GetUserDataAsync(Constants.Constants.botId, userid);
                //var getData = await client.B.GetUserDataAsync(Constants.Constants.botId, userid);
                getData.Data = ar.Serialize();
                getData.ETag = "*";
                var foo = await stateClient.BotState.SetUserDataAsync(Constants.Constants.botId, userid, getData);
                //var foo = await client.Bots.SetUserDataAsync(Constants.Constants.botId, userid, getData);
            }
            //return Request.CreateResponse(foo);
            var response = Request.CreateResponse(HttpStatusCode.Moved);
            response.Headers.Location = new Uri("/loggedin.html", UriKind.Relative);
            return response;
        }
        else
            return Request.CreateResponse(HttpStatusCode.Unauthorized);
    }

请告诉我如何解决以上问题。

pradeep

我如何在LoginController使用服务url或微软应用凭证创建StateClient对象

状态客户端服务的url如下,与通道无关。

        this.BaseUri = new Uri("https://state.botframework.com");

尝试使用helper方法创建您的stateclient:

StateClient stateClient = activity.GetStateClient();