예제 #1
0
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            // for reference: if you ever need to get the correct callbackUri
            // for your app
            // var callbackUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri();


            var client = new OAuth2Client(
                         new Uri(ExpenseTrackerConstants.IdSrvAuthorize));

            string nonce = Guid.NewGuid().ToString() + DateTime.Now.Ticks.ToString();

            var startUrl = client.CreateAuthorizeUrl(
                "native",
                "id_token token",
                 "openid roles",
                 ExpenseTrackerConstants.ExpenseTrackerMobile,
                 "state", nonce);


            WebAuthenticationBroker.AuthenticateAndContinue
                (
                 new Uri(startUrl),
                 new Uri(ExpenseTrackerConstants.ExpenseTrackerMobile),
             null,
             WebAuthenticationOptions.None);

          
        } 
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            // for reference: if you ever need to get the correct callbackUri
            // for your app
            // var callbackUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri();

            var client = new OAuth2Client(
                         new Uri(ExpenseTrackerConstants.IdSrvAuthorizeWp));

            string nonce = Guid.NewGuid().ToString() + DateTime.Now.Ticks.ToString();

            var startUrl = client.CreateAuthorizeUrl(
                "native", //this client corresponds to client config on id srv level
                "id_token token", //Request an id token + access token
                 "openid roles expensetrackerapi", //the access token should contain the id and roles scope
                                //will call the user info end point for this

                                //We no longer use the authorization code as this does not belong to the implicit flow
                                //The WP client isn't a confidential client...
                                //If you do ask for the authorization code, this flow will fail.

                                //Ask for the expensetrackerapi scope to get it included in the access token
                 ExpenseTrackerConstants.ExpenseTrackerMobile,
                 "state", //With state you can pass through values
                 nonce); //Number only used once, this is used by the STS to prevent replay attacks

            WebAuthenticationBroker.AuthenticateAndContinue
                (
                 new Uri(startUrl),
                 new Uri(ExpenseTrackerConstants.ExpenseTrackerMobile),
             null,
             WebAuthenticationOptions.None);
        } 
        private void RequestToken(string scope, string responseType)
        {
            var client = new OAuth2Client(new Uri(Constants.AuthorizeEndpoint));
            var startUrl = client.CreateAuthorizeUrl(
                clientId: "hybridclient",
                responseType: responseType,
                scope: scope,
                redirectUri: "oob://localhost/wpfclient",
                state: "random_state",
                nonce: "random_nonce");

            _login.Show();
            _login.Start(new Uri(startUrl), new Uri("oob://localhost/wpfclient"));
        }
        public async static Task<AuthorizeResponse> DoImplicitFlowAsync(
            Uri endpoint, 
            string clientId,
            string responseType,
            string scope, 
            Uri redirectUri)
        {
            var client = new OAuth2Client(endpoint);
            var state = Guid.NewGuid().ToString("N");
            var nonce = Guid.NewGuid().ToString("N");

            var startUri = client.CreateAuthorizeUrl(
                clientId: clientId,
                responseType: responseType,
                scope: scope,
                redirectUri: redirectUri.AbsoluteUri,
                state: state,
                nonce: nonce, 
                responseMode: "form_post");
                
            try
            {
                var result = await WebAuthenticationBroker.AuthenticateAsync(
                        WebAuthenticationOptions.UseHttpPost,
                        new Uri(startUri));

                if (result.ResponseStatus == WebAuthenticationStatus.Success)
                {
                    return new AuthorizeResponse(result.ResponseData);
                }
                else if (result.ResponseStatus == WebAuthenticationStatus.UserCancel)
                {
                    throw new Exception("User cancelled authentication");
                }
                else if (result.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
                {
                    throw new Exception("HTTP Error returned by AuthenticateAsync() : " + result.ResponseErrorDetail.ToString());
                }
                else
                {
                    throw new Exception("Error returned by AuthenticateAsync() : " + result.ResponseStatus.ToString());
                }
            }
            catch
            {
                // Bad Parameter, SSL/TLS Errors and Network Unavailable errors are to be handled here.
                throw;
            }
        }
		public ActionResult SignIn()
		{
			var client = new OAuth2Client(new Uri(Constants.AuthorizeEndpoint));
			
            var url = client.CreateAuthorizeUrl(
				"implicitclient",
				"id_token",
				"openid email",
				"http://localhost:11716/account/signInCallback",
				"state",
				new Dictionary<string, string>
				{
					{ "nonce", "nonce" },
					{ "response_mode", "form_post" }
				});
				
            return Redirect(url);
		}
        private void RequestToken(string scope, string responseType)
        {
            var additional = new Dictionary<string, string>
            {
                { "nonce", "nonce" }
            };

            var client = new OAuth2Client(new Uri(Constants.AuthorizeEndpoint));
            var startUrl = client.CreateAuthorizeUrl(
                "implicitclient",
                responseType,
                scope,
                "oob://localhost/wpfclient",
                "state",
                additional);


            _login.Show();
            _login.Start(new Uri(startUrl), new Uri("oob://localhost/wpfclient"));
        }
        public static void DoImplicitFlowAsync(
            Uri endpoint,
            string clientId,
            string responseType,
            string scope,
            Uri redirectUri)
        {
            var client = new OAuth2Client(endpoint);
            var state = Guid.NewGuid().ToString("N");
            var nonce = Guid.NewGuid().ToString("N");

            var startUri = client.CreateAuthorizeUrl(
                clientId: clientId,
                responseType: responseType,
                scope: scope,
                redirectUri: redirectUri.AbsoluteUri,
                state: state,
                nonce: nonce);

            try
            {

                // On Windows Phone 8.1, the AuthenticateAsync method isn't implemented on 
                // the WebAuthenticationBroker.  Therefor, AuthenticateAndContinue is used.  
                // 
                // Callback = ContinueWebAuthentication in MainPage.xaml.cs

                WebAuthenticationBroker.AuthenticateAndContinue(
                    new Uri(startUri),
                   redirectUri,
                     null,
                     WebAuthenticationOptions.None
                    ); 
            }
            catch
            {
                // Bad Parameter, SSL/TLS Errors and Network Unavailable errors are to be handled here.
                throw;
            }
        }
        private void RequestToken(string scope, string responseType)
        {
            var client = new OAuth2Client(new Uri(Constants.AuthorizeEndpoint));
            var startUrl = client.CreateAuthorizeUrl(
                clientId: "implicitclient",
                responseType: responseType,
                scope: scope,
                redirectUri: "oob://localhost/wpfclient",
                state: "random_state",
                nonce: "random_nonce" /**,
                loginHint: "alice",
                acrValues: "idp:Google b c" **/);

            _login.Show();
            _login.Start(new Uri(startUrl), new Uri("oob://localhost/wpfclient"));
        }