var querystringFromUrlReferrer= HttpUtility.ParseQueryString(HttpContext.Request.UrlReferrer.Query); var valueYouWant= querystringFromUrlReferrer["NameOfVarToGetFromQuerystring"];
There you go – very helpful…
var querystringFromUrlReferrer= HttpUtility.ParseQueryString(HttpContext.Request.UrlReferrer.Query); var valueYouWant= querystringFromUrlReferrer["NameOfVarToGetFromQuerystring"];
There you go – very helpful…
You will have to have a Google account to use reCAPTCHA.
Log into the google account you want to tie your reCAPTCHA keys to and navigate to:
https://www.google.com/recaptcha/admin#list
Under the ‘Register a new site’ section of that page,follow the instructions and set up a separate key set for each of your development,test, and production environments – including one specifically for ‘localhost’ or 127.0.0.1 so you can test locally.
Add the public and private keys you just created on the Google site to your web.config:
<add key="ReCaptchaPrivateKey" value="yourPrivateKey"/> <add key="ReCaptchaPublicKey" value="yourPublicKey"/>
Add a property for yourPublicKey to your viewmodel and load it from the web.config in the get controller action, passing it into the view.
Add this script in the head section of your layout or view page:
<script src="https://www.google.com/recaptcha/api.js?render=@layoutModel.yourPublicKey"></script>
There are two steps for the view page where you want to display the reCaptcha box:
Add this to the view where you want the reCaptcha box to display:
<div class="g-recaptcha" data-sitekey="@Model.YourPublicKey">
And add this script at the bottom of that view:
<script src='https://www.google.com/recaptcha/api.js'></script>
You will need some code in the post action of your controller to hit the Google reCaptcha service and get a response if it appears this is a valid person – something like this:
var response = Request["g-recaptcha-response"]; var reCaptchaSecretKey = ConfigurationManager.AppSettings["yourPrivateKey"]; var webClient = new WebClient(); var resultFromGoogle = webClient.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", reCaptchaSecretKey , response)); var parsedResponseFromGoogle = JObject.Parse(resultFromGoogle); var thisIsARealPersonNotARobot = (bool)parsedResponseFromGoogle.SelectToken("success");
With that result in hand, you can decide how to handle a success or failure.
I noticed that reCAPTCHA tried to send it’s request through TLS1.1 and our site will not allow it – we require TLS 1.2, so I had to add a directive to force it to only use 1.2 with this setting at the top of the HTTPPost controller action:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Thanks for reading and happy coding,
-Jim
If a user comes to a page with dynamically loaded dropdownlists using the back button – use this to force the page to reload so the dynamic controls reload their content to initial state:
if (performance.navigation.type === 2) { location.reload(true);
@Html.DropDownListFor(m => m.SelectedValue,
new SelectList(Model.CollectionOfItemsForDropdown, "ValueField", "NameField"),
@Model.CollectionOfItemsForDropdown.Count > 1
? (object)new { @class = "form-control", required = "true", }
: new { @class = "form-control", required = "true", disabled = "disabled" })
You have to use the conditional operator here for the anonymous objects because the dropdown list will be disabled if the word ‘disabled’ is rendered in the tag in any way.
Here is a simple way to download a file to a .net user control from a newer MVC controller.
The Controller code:
public FileResult DownloadSweetFile()
{
var downloadDirectory = _appSettings.PathToFile;
var filePathAndName = Path.Combine(downloadDirectory, "MySweetFile.pdf");
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "MySweetFile.pdf",
Inline = false, //NOTE: This forces always prompting the user to download, not open file in the browser
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(filePathAndName, "application/pdf");
}
I used a button on the page to fire the javascript
button type="button" id="btnSweetDownload" onclick="downloadSweetFile();" Click here to download a Sweet File!
(I removed the button tag so this will show correctly)
And here is the javascript to use in the .ascx file:
function downloadSweetFile() {
var url = '/MyControllerName/DownloadSweetFile';
window.location = url;
};