Esempio n. 1
0
        public async Task <ActionResult> Index(string username, string password)
        {
            ViewBag.Username    = username;
            ViewBag.Password    = password;
            ViewBag.ApiResult   = "";
            ViewBag.AccessToken = "";

            if (!string.IsNullOrEmpty(username))
            {
                var authServer = new AuthorizationServerDescription
                {
                    AuthorizationEndpoint = new Uri(Paths.AuthorizationServerBaseAddress + Paths.AuthorizePath),
                    TokenEndpoint         = new Uri(Paths.AuthorizationServerBaseAddress + Paths.TokenPath)
                };
                var webServerClient = new WebServerClient(authServer, Clients.Client1.Id, Clients.Client1.Secret);

                var state = webServerClient.ExchangeUserCredentialForToken(username, password, new[] { "test1", "test2", "test3" });
                var token = state.AccessToken;
                ViewBag.AccessToken = token;

                var client    = new HttpClient(webServerClient.CreateAuthorizingHandler(token));
                var apiResult = await client.GetStringAsync(Paths.ResourceUserApiPath);

                ViewBag.ApiResult = apiResult;
            }
            return(View());
        }
 public ActionResult Index(string username, string password)
 {
     //var user = UserClient.Instance.ValidateUser(username, password, Shumi.Users.PasswordFormat.Cleartext);
     //if (user != null)
     //{
     //    FormsAuthentication.SetAuthCookie(user.Name, false);
     //    Authorization = _client.ExchangeUserCredentialForToken(username, password);
     //}
     if (Authorization == null)
     {
         try
         {
             Authorization = _client.ExchangeUserCredentialForToken(username, password);
         }
         catch (ProtocolException ex)
         {
             ViewBag.LoginError = "请求被拒绝,请检查用户名或密码是否有误!";
         }
     }
     else
     {
         var request = new HttpRequestMessage(new HttpMethod("GET"), "http://demo.openapi.cn/bookcates");
         using (var httpClient = new HttpClient(_client.CreateAuthorizingHandler(Authorization)))
         {
             using (var resourceResponse = httpClient.SendAsync(request))
             {
                 ViewBag.Result = resourceResponse.Result.Content.ReadAsStringAsync().Result;
             }
         }
     }
     return(View(Authorization));
 }
Esempio n. 3
0
        protected void Button3_Click(object sender, EventArgs e)
        {
            var authorizationState = _webServerClient.ExchangeUserCredentialForToken(Username.Text, Password.Text, new[] { "bio", "notes" });

            if (authorizationState != null)
            {
                AccessToken.Text = authorizationState.AccessToken;
            }
        }
Esempio n. 4
0
            public void WhenExchangeUserCredentialForTokenWithUnknownClientApplication_ThenThrows()
            {
                CreateAuthZTokenClient("foo", "bar");

                Assert.Throws <ProtocolException>(DnoaInvalidRequestErrorMessage,
                                                  () => authZTokenClient.ExchangeUserCredentialForToken("ausername", "apassword", new[]
                {
                    AccessScope.Profile
                }));
            }
Esempio n. 5
0
        public ActionResult Login(string username, string password, string returnUrl = "")
        {
            InitializeWebServerClient();

            //var returnUrl = Request.QueryString["ReturnUrl"];

            var state = _webServerClient.ExchangeUserCredentialForToken(username, password, scopes: new string[] { "bio" });

            _tokenDir[state.AccessToken] = state.RefreshToken;

            Response.Cookies.Add(new HttpCookie("access_token", state.AccessToken));
            if (!string.IsNullOrWhiteSpace(returnUrl))
            {
                return(Redirect(returnUrl));
            }

            return(Redirect("~/Home"));
        }
        public ViewResult ResourceOwnerCredentialsGrant(ResourceOwnerCredentialsGrantViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                try
                {
                    // Create the client with which we will be connecting to the server.
                    var webServerClient = new WebServerClient(this.AuthorizationServerDescription, clientIdentifier: model.ClientId);

                    // The scope that we request for the user. Note: this can also be null if we don't want to request any specific
                    // scope or more than one scope if we want to request an access token that is valid for several scopes
                    var userScopes = OAuthUtilities.SplitScopes(model.Scope ?? string.Empty);

                    // Request a new user access token for the specified user and the specified scopes (http://tools.ietf.org/html/draft-ietf-oauth-v2-31#page-35)
                    this.ViewBag.AccessToken = webServerClient.ExchangeUserCredentialForToken(model.Username, model.Password, userScopes);
                }
                catch (Exception ex)
                {
                    this.ViewBag.Exception = ex;
                }
            }

            return(this.View(model));
        }
        private static void RequestToken()
        {
            var state = _webServerClient.ExchangeUserCredentialForToken("1", "000000", scopes: new string[] { "bio" });

            _accessToken = state.AccessToken;
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            // Remove this for production it just eats tee  Https invalid certificate error
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

            // usung DotNetOpenAuth lib here but any Auth2 lib should work
            var authServer = new AuthorizationServerDescription
            {
                TokenEndpoint   = new Uri(APIURL + "/oauth/token"),
                ProtocolVersion = ProtocolVersion.V20
            };

            // get our token
            var client = new WebServerClient(authServer, APIURL + "/oauth/token");
            var auth   = client.ExchangeUserCredentialForToken("demo123", "demo123");

            Console.WriteLine(String.Format("Access Token Set to: {0} ", auth.AccessToken));

            // create credit keysale object and transform to json
            var keySale = new { amount = 19.95, credit_card = new { number = "4111111111111111", expiration_month = 12, expiration_year = 20 } };
            var json    = JsonConvert.SerializeObject(keySale);

            Console.WriteLine(Environment.NewLine + string.Format("Json Card String: {0}", json));

            byte[] buf = Encoding.UTF8.GetBytes(json);

            // build request
            var request = (HttpWebRequest)WebRequest.Create(APIURL + "/v1/transactions/sale/keyed");

            request.ContentType   = @"application/json";
            request.Method        = "POST";
            request.ContentLength = buf.Length;

            // add our auth token
            client.AuthorizeRequest(request, auth);
            // buffer the body
            request.GetRequestStream().Write(buf, 0, buf.Length);

            try
            {
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine("Response: ");
                Console.WriteLine(Environment.NewLine);
                var response = (HttpWebResponse)request.GetResponse();
                using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    Console.WriteLine(reader.ReadToEnd());
                    Console.WriteLine(Environment.NewLine);
                }
            }
            catch (WebException e)
            {
                Console.WriteLine(string.Format("Error: {0}", e.Message));
                Console.WriteLine(Environment.NewLine);
            }
            finally
            {
                Console.WriteLine(Environment.NewLine + "Hit any key to quit: ");
                Console.Read();
            }
        }
Esempio n. 9
0
    private static void RequestToken()
    {
        var state = _webServerClient.ExchangeUserCredentialForToken("test", "test", new[] { "bio", "notes" });

        _accessToken = state.AccessToken;
    }
Esempio n. 10
0
        private static void RequestToken()
        {
            var state = _webServerClient.ExchangeUserCredentialForToken("*****@*****.**", "12345Ankara!", scopes: new string[] { "USER_COMPLIANCE", "USER_FINANCIAL", "USER_REGISTRATION", "PUBLIC_DATA" });

            _accessToken = state.AccessToken;
        }
Esempio n. 11
0
        private static void RequestToken()
        {
            var state = _webServerClient.ExchangeUserCredentialForToken("Usuário", "Senha", scopes: new string[] { "notas", "administrador" });

            _accessToken = state.AccessToken;
        }
Esempio n. 12
0
        private static void RequestToken()
        {
            var state = _webServerClient.ExchangeUserCredentialForToken("testuser", "testuser");

            _accessToken = state.AccessToken;
        }