Esempio n. 1
0
        public async Task <HttpResponseMessage> PostAuthorize([FromBody] AuthorizeArgs a)
        {
            var cookie = SecureUrlToken.Decode <ResumptionCookie>(a.state);

            if (!string.IsNullOrEmpty(a.error))
            {
                await Conversation.ResumeAsync(cookie, new AuthenticationResultModel(cookie.GetMessage()) { Error = a.error, ErrorDescription = a.error_description });

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("<html><head><script type='text/javascript'>window.close();</script></head><body>An error occurred during authentication.  You can close this browser window</body></html>", Encoding.UTF8, "text/html")
                });
            }

            // Get access token
            var authContext = new AuthenticationContext(ConfigurationManager.AppSettings["Authority"]);
            var authResult  = await authContext.AcquireTokenByAuthorizationCodeAsync(
                a.code,
                new Uri(this.Request.RequestUri.GetLeftPart(UriPartial.Path)),
                new ClientCredential(
                    ConfigurationManager.AppSettings["ClientId"],
                    ConfigurationManager.AppSettings["ClientSecret"]));

            var upn = authResult?.UserInfo?.DisplayableId;

            var result = new AuthenticationResultModel(cookie.GetMessage())
            {
                AccessToken = authResult.IdToken
            };

            if (upn == cookie.GetMessage().From.Id)
            {
                await Conversation.ResumeAsync(cookie, result);

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("<html><head><script type='text/javascript'>window.close();</script></head><body>You can close this browser window</body></html>", Encoding.UTF8, "text/html")
                });
            }
            else
            {
                var rnd = new Random();
                result.SecurityKey = string.Join("", Enumerable.Range(0, 6).Select(i => rnd.Next(10).ToString()));
                await Conversation.ResumeAsync(cookie, result);

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent($"<html><head></head><body><!--We can't auto-auth you because {upn} != {cookie.GetMessage().From.Id}. -->Please copy and paste this key into the conversation with the bot: {result.SecurityKey}.</body></html>", Encoding.UTF8, "text/html")
                });
            }
        }
Esempio n. 2
0
 public static Task <string> Process(Uri requestUri, AuthorizeArgs args)
 {
     return(Process(requestUri, args.state, args.code, args.error, args.error_description));
 }