Esempio n. 1
0
        //
        //  Inherit methods from parent class

        override public object Authorize(HttpRequest Request, string accountName)
        {
            try
            {
                var uri   = Request.Url.ToString();
                var code  = Request["code"];
                var error = Request["error"];

                if (!string.IsNullOrEmpty(error))
                {
                    if (error == "access_denied")
                    {
                        return(new UserRejectException());
                    }
                    else
                    {
                        return(new UnknownException(error));
                    }
                }

                // перед авторизацией нужно обнулить
                CalendarService = null;

                if (code != null)
                {
                    string redirectUri = uri.Substring(0, uri.IndexOf("?"));
                    var    token       = CodeFlow.ExchangeCodeForTokenAsync(accountName, code, redirectUri, CancellationToken.None).Result;
                    string state       = Request["state"];
                    var    result      = AuthWebUtility.ExtracRedirectFromState(CodeFlow.DataStore, accountName, state);
                    return(result);
                }
                else
                {
                    string redirectUri = uri;
                    string state       = "ostate_";// Guid.NewGuid().ToString("N");
                    var    result      = new AuthorizationCodeWebApp(CodeFlow, redirectUri, state).AuthorizeAsync(accountName, CancellationToken.None).Result;
                    if (result.RedirectUri != null)
                    {
                        return(result);
                    }
                    else
                    {
                        CalendarService = new CalendarService(new BaseClientService.Initializer()
                        {
                            HttpClientInitializer = result.Credential,
                            ApplicationName       = APPLICATION_NAME
                        });
                        // alright
                        return("OK");
                    }
                }
            }
            catch (Exception ex)
            {
                return(ex);
            }

            return(null);
        }