Ejemplo n.º 1
0
        /// <summary>
        /// 分析認證通過後的字串,並且取得使用者詳細資訊,最後需要建立 Account 物件
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private static async Task <Account> ConvertCodeToAccount(string code)
        {
            OAuth2AuthenticatorParameter fooPara = new OAuth2AuthenticatorParameter();

            #region 依據指定的 OAuth2 認證方式,取得該認證要用到的相關參數
            if (AuthenticationHelper.OAuthType == OAuthTypeEnum.Google)
            {
                fooPara = AuthenticationHelper.OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Google);
            }
            else if (AuthenticationHelper.OAuthType == OAuthTypeEnum.Facebook)
            {
                fooPara = AuthenticationHelper.OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Facebook);
            }
            #endregion

            var fooauthorizeUrl   = fooPara.authorizeUrl;
            var fooredirectUrl    = fooPara.redirectUrl;
            var fooaccessTokenUrl = fooPara.accessTokenUrl;
            var fooclientId       = fooPara.clientId;
            var fooclientSecret   = fooPara.clientSecret;
            var fooscope          = fooPara.scope;

            #region 取得 使用者詳細資訊
            var httpClient = new System.Net.Http.HttpClient();
            var content    = new FormUrlEncodedContent(new Dictionary <string, string> {
                { "code", code },
                { "client_id", fooPara.clientId },
                { "client_secret", fooPara.clientSecret },
                { "redirect_uri", fooPara.redirectUrl },
                { "grant_type", "authorization_code" },
            });
            var accessTokenResponse = await httpClient.PostAsync((fooPara.accessTokenUrl), content);

            var fooRetStr = await accessTokenResponse.Content.ReadAsStringAsync();

            #endregion

            var FooresponseDict = JsonConvert.DeserializeObject <AccessTokenResponse>(fooRetStr);
            var responseDict    = new Dictionary <string, string>
            {
                { Constants.access_tokenAccountProperty, FooresponseDict.access_token },
                { Constants.expires_inAccountProperty, FooresponseDict.expires_in },
                { Constants.token_typeAccountProperty, FooresponseDict.token_type },
                //{"id_token",FooresponseDict.id_token},
            };

            return(new Account(null, responseDict));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// OnElementChanged method is called when the corresponding Xamarin.Forms control is created.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnElementChanged(ElementChangedEventArgs <Page> e)
        {
            base.OnElementChanged(e);

            if (_isShown)
            {
                return;
            }
            _isShown = true;

            OAuth2Authenticator          auth;
            OAuth2AuthenticatorParameter fooPara = new OAuth2AuthenticatorParameter();

            #region 依據指定的 OAuth2 認證方式,取得該認證要用到的相關參數
            if (AuthenticationHelper.OAuthType == OAuthTypeEnum.Google)
            {
                fooPara = AuthenticationHelper.OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Google);
            }
            else if (AuthenticationHelper.OAuthType == OAuthTypeEnum.Facebook)
            {
                fooPara = AuthenticationHelper.OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Facebook);
            }
            #endregion

            var fooauthorizeUrl   = fooPara.authorizeUrl;
            var fooredirectUrl    = fooPara.redirectUrl;
            var fooaccessTokenUrl = fooPara.accessTokenUrl;
            var fooclientId       = fooPara.clientId;
            var fooclientSecret   = fooPara.clientSecret;
            var fooscope          = fooPara.scope;
            auth = new OAuth2Authenticator(
                fooclientId,
                fooclientSecret,
                fooscope,
                new Uri(fooauthorizeUrl),
                new Uri(fooredirectUrl),
                new Uri(fooaccessTokenUrl));

            auth.Completed += OnAuthenticationCompleted;

            // Display the UI
            var activity = Context as Activity;
            activity?.StartActivity(auth.GetUI(activity));
        }
Ejemplo n.º 3
0
        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            if (_isShown)
            {
                return;
            }
            _isShown = true;

            OAuth2Authenticator          auth;
            OAuth2AuthenticatorParameter fooPara = new OAuth2AuthenticatorParameter();

            #region 依據指定的 OAuth2 認證方式,取得該認證要用到的相關參數
            if (AuthenticationHelper.OAuthType == OAuthTypeEnum.Google)
            {
                fooPara = AuthenticationHelper.OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Google);
            }
            else if (AuthenticationHelper.OAuthType == OAuthTypeEnum.Facebook)
            {
                fooPara = AuthenticationHelper.OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Facebook);
            }
            #endregion

            var fooauthorizeUrl   = fooPara.authorizeUrl;
            var fooredirectUrl    = fooPara.redirectUrl;
            var fooaccessTokenUrl = fooPara.accessTokenUrl;
            var fooclientId       = fooPara.clientId;
            var fooclientSecret   = fooPara.clientSecret;
            var fooscope          = fooPara.scope;
            auth = new OAuth2Authenticator(
                fooclientId,
                fooclientSecret,
                fooscope,
                new Uri(fooauthorizeUrl),
                new Uri(fooredirectUrl),
                new Uri(fooaccessTokenUrl));

            auth.Completed += OnAuthenticationCompleted;

            // Display the UI
            PresentViewController(auth.GetUI(), true, null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 進行 OAuth2 的認證
        /// </summary>
        /// <returns></returns>
        private async Task <string> AuthenticateUsingWebAuthenticationBroker()
        {
            OAuth2Authenticator          auth;
            OAuth2AuthenticatorParameter fooPara = new OAuth2AuthenticatorParameter();

            #region 依據指定的 OAuth2 認證方式,取得該認證要用到的相關參數
            if (AuthenticationHelper.OAuthType == OAuthTypeEnum.Google)
            {
                fooPara = AuthenticationHelper.OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Google);
            }
            else if (AuthenticationHelper.OAuthType == OAuthTypeEnum.Facebook)
            {
                fooPara = AuthenticationHelper.OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Facebook);
            }
            #endregion

            var fooauthorizeUrl   = fooPara.authorizeUrl;
            var fooredirectUrl    = fooPara.redirectUrl;
            var fooaccessTokenUrl = fooPara.accessTokenUrl;
            var fooclientId       = fooPara.clientId;
            var fooclientSecret   = fooPara.clientSecret;
            var fooscope          = fooPara.scope;

            #region 建立要取得 OAuth2 認證的URL
            var fooOAuthLoginUrl = fooauthorizeUrl + "?client_id=" +
                                   Uri.EscapeDataString(fooclientId);
            fooOAuthLoginUrl += "&redirect_uri=" + Uri.EscapeDataString(fooredirectUrl);
            fooOAuthLoginUrl += "&response_type=code";
            fooOAuthLoginUrl += "&scope=" + Uri.EscapeDataString(fooscope);
            #endregion

            var startUri = new Uri(fooOAuthLoginUrl);

            var webAuthenticationResult =
                await
                WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startUri,
                                                          new Uri(fooredirectUrl));

            return(webAuthenticationResult.ResponseStatus != WebAuthenticationStatus.Success ? null : webAuthenticationResult.ResponseData.Substring(webAuthenticationResult.ResponseData.IndexOf('=') + 1));
        }