/// <inheritdoc /> public async Task<ModelBindingResult> BindModelAsync([NotNull] ModelBindingContext bindingContext) { if (bindingContext.ModelType != typeof(IFormCollection) && bindingContext.ModelType != typeof(FormCollection)) { return null; } object model = null; var request = bindingContext.OperationBindingContext.HttpContext.Request; if (request.HasFormContentType) { var form = await request.ReadFormAsync(); if (bindingContext.ModelType.IsAssignableFrom(form.GetType())) { model = form; } else { var formValuesLookup = form.ToDictionary(p => p.Key, p => p.Value); model = new FormCollection(formValuesLookup, form.Files); } } else { model = new FormCollection(new Dictionary<string, string[]>()); } return new ModelBindingResult(model, bindingContext.ModelName, isModelSet: true); }
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var response = new HttpResponseMessage(); if (request.RequestUri.AbsoluteUri.StartsWith("https://accounts.google.com/o/oauth2/token")) { var formData = new FormCollection(await FormReader.ReadFormAsync(await request.Content.ReadAsStreamAsync())); if (formData["grant_type"] == "authorization_code") { if (formData["code"] == "ValidCode") { if (formData["redirect_uri"] != null && formData["redirect_uri"].EndsWith("signin-google") && formData["client_id"] == "[ClientId]" && formData["client_secret"] == "[ClientSecret]") { response.Content = new StringContent("{\"access_token\":\"ValidAccessToken\",\"refresh_token\":\"ValidRefreshToken\",\"token_type\":\"Bearer\",\"expires_in\":\"1200\",\"id_token\":\"Token\"}", Encoding.UTF8, "application/json"); } } } } else if (request.RequestUri.AbsoluteUri.StartsWith("https://www.googleapis.com/plus/v1/people/me")) { if (request.Headers.Authorization.Parameter == "ValidAccessToken") { response.Content = new StringContent("{ \"kind\": \"plus#person\",\n \"etag\": \"\\\"YFr-hUROXQN7IOa3dUHg9dQ8eq0/2hY18HdHEP8NLykSTVEiAhkKsBE\\\"\",\n \"gender\": \"male\",\n \"emails\": [\n {\n \"value\": \"[email protected]\",\n \"type\": \"account\"\n }\n ],\n \"objectType\": \"person\",\n \"id\": \"106790274378320830963\",\n \"displayName\": \"AspnetvnextTest AspnetvnextTest\",\n \"name\": {\n \"familyName\": \"AspnetvnextTest\",\n \"givenName\": \"FirstName\"\n },\n \"url\": \"https://plus.google.com/106790274378320830963\",\n \"image\": {\n \"url\": \"https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50\"\n },\n \"isPlusUser\": true,\n \"language\": \"en\",\n \"circledByCount\": 0,\n \"verified\": false\n}\n", Encoding.UTF8, "application/json"); } else { response.Content = new StringContent("{\"error\":{\"message\":\"Invalid OAuth access token.\",\"type\":\"OAuthException\",\"code\":190}}"); } } return response; }
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var response = new HttpResponseMessage(); if (request.RequestUri.AbsoluteUri.StartsWith("https://login.live.com/oauth20_token.srf")) { var formData = new FormCollection(await FormReader.ReadFormAsync(await request.Content.ReadAsStreamAsync())); if (formData["grant_type"] == "authorization_code") { if (formData["code"] == "ValidCode") { if (formData["redirect_uri"] != null && formData["redirect_uri"].EndsWith("signin-microsoft") && formData["client_id"] == "[ClientId]" && formData["client_secret"] == "[ClientSecret]") { response.Content = new StringContent("{\"token_type\":\"bearer\",\"expires_in\":3600,\"scope\":\"wl.basic\",\"access_token\":\"ValidAccessToken\",\"refresh_token\":\"ValidRefreshToken\",\"authentication_token\":\"ValidAuthenticationToken\"}"); } } } } else if (request.RequestUri.AbsoluteUri.StartsWith("https://apis.live.net/v5.0/me")) { if (request.Headers.Authorization.Parameter == "ValidAccessToken") { response.Content = new StringContent("{\r \"id\": \"fccf9a24999f4f4f\", \r \"name\": \"AspnetvnextTest AspnetvnextTest\", \r \"first_name\": \"AspnetvnextTest\", \r \"last_name\": \"AspnetvnextTest\", \r \"link\": \"https://profile.live.com/\", \r \"gender\": null, \r \"locale\": \"en_US\", \r \"updated_time\": \"2013-08-27T22:18:14+0000\"\r}"); } else { response.Content = new StringContent("{\r \"error\": {\r \"code\": \"request_token_invalid\", \r \"message\": \"The access token isn't valid.\"\r }\r}", Encoding.UTF8, "text/javascript"); } } return response; }
public ActionResult ReturnFileContent(FormCollection form) { var file = form.Files.GetFile("File"); using (var reader = new StreamReader(file.OpenReadStream())) { var fileContent = reader.ReadToEnd(); return Content(fileContent); } }
public IActionResult GridViewReplacementWithInplaceEditing(FormCollection collection) { ArtistSkill skill = new ArtistSkill(); skill.ArtistId = Int32.Parse(collection["item.ArtistId"]); skill.ArtistTalentId = Int32.Parse(collection["item.ArtistTalentId"]); skill.TalentName = collection["item.TalentName"]; skill.SkillLevel = Int32.Parse(collection["item.SkillLevel"]); skill.Details = collection["item.Details"]; skill.Styles = collection["item.Styles"]; _Uow.ArtistSkillRepository.Update(skill); return RedirectToAction("GridViewReplacementWithInplaceEditing"); }
public async Task FormCollectionModelBinder_InvalidType_BindFails() { // Arrange var formCollection = new FormCollection(new Dictionary<string, string[]> { { "field1", new string[] { "value1" } }, { "field2", new string[] { "value2" } } }); var httpContext = GetMockHttpContext(formCollection); var bindingContext = GetBindingContext(typeof(string), httpContext); var binder = new FormCollectionModelBinder(); // Act var result = await binder.BindModelAsync(bindingContext); // Assert Assert.Null(result); }
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var response = new HttpResponseMessage(); if (request.RequestUri.AbsoluteUri.StartsWith("https://api.twitter.com/oauth/access_token")) { var formData = new FormCollection(await FormReader.ReadFormAsync(await request.Content.ReadAsStreamAsync())); if (formData["oauth_verifier"] == "valid_oauth_verifier") { if (_requestTokenEndpointInvoked) { var response_Form_data = new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("oauth_token", "valid_oauth_token"), new KeyValuePair<string, string>("oauth_token_secret", "valid_oauth_token_secret"), new KeyValuePair<string, string>("user_id", "valid_user_id"), new KeyValuePair<string, string>("screen_name", "valid_screen_name"), }; response.Content = new FormUrlEncodedContent(response_Form_data); } else { response.StatusCode = HttpStatusCode.InternalServerError; response.Content = new StringContent("RequestTokenEndpoint is not invoked"); } } } else if (request.RequestUri.AbsoluteUri.StartsWith("https://api.twitter.com/oauth/request_token")) { var response_Form_data = new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("oauth_callback_confirmed", "true"), new KeyValuePair<string, string>("oauth_token", "valid_oauth_token"), new KeyValuePair<string, string>("oauth_token_secret", "valid_oauth_token_secret") }; _requestTokenEndpointInvoked = true; response.Content = new FormUrlEncodedContent(response_Form_data); } return response; }
public async Task FormCollectionModelBinder_ValidType_BindSuccessful() { // Arrange var formCollection = new FormCollection(new Dictionary<string, string[]> { { "field1", new string[] { "value1" } }, { "field2", new string[] { "value2" } } }); var httpContext = GetMockHttpContext(formCollection); var bindingContext = GetBindingContext(typeof(FormCollection), httpContext); var binder = new FormCollectionModelBinder(); // Act var result = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(result); var form = Assert.IsAssignableFrom<IFormCollection>(result.Model); Assert.Equal(2, form.Count); Assert.Equal("value1", form["field1"]); Assert.Equal("value2", form["field2"]); }