integrating recaptcha (with custom look) with asp.net - c#

Im using asp.net/c# weborms. I've added recaptcha to the form and used what is on their site. It needs a custom look hence it's like this:
<div id="recaptcha_widget" style="display:none">
<div id="recaptcha_image"></div>
<div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>
<span class="recaptcha_only_if_image">Enter the words above:</span>
<span class="recaptcha_only_if_audio">Enter the numbers you hear:</span>
<input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
<div>Get another CAPTCHA</div>
<div class="recaptcha_only_if_image">Get an audio CAPTCHA</div>
<div class="recaptcha_only_if_audio">Get an image CAPTCHA</div>
<div>Help</div>
</div>
<script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=your_public_key">
</script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=your_public_key"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
what do i need to do in the button_click method in the code behind iof the form to check if the words eneterd by the user is correct. same for audio.
Thanks

Why don't you use the control that is delivered with reCaptcha? Here is the control and a quickstart.
reCaptcha Quickstart & Control

Like other validations you just need to check if(Page.IsValid) in behind code. just note that you have to add recaptcha control in your code and then add your customs them.
<recaptcha:RecaptchaControl ID="recaptcha" runat="server" PublicKey="your_public_key"
PrivateKey="Your_private_key" Theme="custom" />
<div id="recaptcha_widget" style="display:none">
<div id="recaptcha_image"></div>
<div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>
...

Related

Selenium cannot find Cookie Accept button

Does anybody have any ideas why Selenium cannot find the Cookie Accept button defined below. I have tried a number of methods and wait strategies but nothing seems to work? When the Cookie banner comes up the main window behind greys out. URL: https://portal.everybody.org.uk/lhweb/identity/login?
HTML
<div class="xn-auth-form xn-login-form">
<div id="IJFIX74FEMS85U04VNHN9XD1G6T36PB6" class="xn-component" style=""><xn-cookiebanner-component params="{"newcomponent":"true"}">
<div data-bind="css: { 'xn-modal': isModal, 'xn-visible': isVisible}" class="xn-modal xn-visible">
<div class="xn-cookie-banner-header">
<div class="xn-cookie-banner-name">
<div class="xn-cookie-banner-title">Cookies Policy</div>
<div class="xn-name" style="word-break:keep-all">
<span> Cookies created and stored for this site are fundamental to its functionality and performance. Cookies are not used for marketing purposes, nor are any personally identifiable cookies stored.</span>
</div>
</div>
<div class="xn-buttons">
<button class="xn-button xn-cta" data-bind="click: confirm"><span data-bind="text: confirmationYes">Accept</span></button>
</div>
</div>
</div></xn-cookiebanner-component></div>
<form name="form" method="post" action="/lhweb/identity/login?signin=d367e21d0f8af2faf156cb566b51b78a" novalidate="novalidate">
<input type="hidden" name="idsrv.xsrf" value="GgTgFinGkFku2cMRPEcwMy9qv4fh3dUGJV26u-jv6Os0VF-1gFmP_JNHPRRz_lywbkTZ4XAaUs5atoqRHKu56gAHU_lfv-ZZplq476ZcpfQ" autocomplete="off">
C#
IWebElement button1 = driver.FindElement(By.XPath("/html/body/div[3]/div/div/div/xn-cookiebanner-component/div/div/div[2]/button"));
IWebElement button2 = driver.FindElement(By.Name("Accept"));
You can use the below locators:
XPATH:
.//*[#class='xn-button xn-cta' and #data-bind='click: confirm']
CSS SELECTOR:
.xn-cookie-banner-header .xn-button.xn-cta

Implementing simple validation on the client-side in C# ASP.NET Core 2.2

Adding simple validation on the client-side in C# ASP.NET Core 2.2
I've read MS docs on validation and decided that it's most suitable to apply client-side validation. However, in the provided example, they show something with jQuery and I was under the impression that jQuery was getting a bit old by now. Also, we're spoiled with the nice tools from MS that will automagically do stuff for us. (I thought that the span with attribute asp-validation-for and div with attribute asp-validation-summary implied that the magic would be there. It wasn't as it turns out.)
My code is as shown below.
<div class="row">
<div class="col-md-4">
<form asp-controller="Security"
asp-action="Register"
method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="#Model.Email" class="control-label"></label>
<input asp-for="#Model.Email" class="form-control" />
<span asp-validation-for="#Model.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="#Model.Info" class="control-label"></label>
<input asp-for="#Model.Info" class="form-control" />
<span asp-validation-for="#Model.Info" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Send" class="btn btn-primary" />
</div>
</form>
</div>
</div>
I'd like to know what I'm missing to make the validation work properly (i.e. getting an error message if not both fields are filled upon submission of the form as well as the form submitting being cancelled). Alternatively, of it's something fairly basic I'm missing, I'd like to get a hint on appropriate search words. Please note that I'm only looking for the visuals: client-side messages, no relation to the back-end handler of the input.
I've seen some questions on the subject but since those seem not to have a resolution approved, I feel that it's best to ask in order to do things the right way instead of a guessing game.
You need to add reference to validation file. Check whether you have _ValidationScriptsPartial.cshtml in your Views/Shared folder.
<environment include="Development">
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
</environment>
<environment exclude="Development">
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.17.0/jquery.validate.min.js"
asp-fallback-src="~/Identity/lib/jquery-validation/dist/jquery.validate.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator"
crossorigin="anonymous"
integrity="sha384-rZfj/ogBloos6wzLGpPkkOr/gpkBNLZ6b6yLy4o+ok+t/SAKlL5mvXLr0OXNi1Hp">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.9/jquery.validate.unobtrusive.min.js"
asp-fallback-src="~/Identity/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"
crossorigin="anonymous"
integrity="sha384-ifv0TYDWxBHzvAk2Z0n8R434FL1Rlv/Av18DXE43N/1rvHyOG4izKst0f2iSLdds">
</script>
</environment>
Add below code in view,
#section Scripts {
<partial name="_ValidationScriptsPartial" />
}

Form post to another page

I am fairly new to C# and asp.net and am having trouble showing something submitted in a form. The form element is a tag. The drop-down info is pulled from a data base and displays correctly. It's just getting it to post to another page after the form is submitted is where I am having the problem. Any help would be appreciated.
Contact.aspx:
<form action="Default.aspx" method="post" data-transition="pop">
<div data-role="fieldcontain">
<label for="topEmails">Business Entity ID:</label>
<select name="topEmails" id="topEmails" data-native-menu="false"
runat="server">
</select>
</div>
<input type="submit" data-iconpos="right" data-inline="true"
data-icon="plus" name="sendMessage" id="sendMessage" value="Send Info">
</form>
Contact.aspx.cs:
AdventureWorks2012DataContext db = new AdventureWorks2012DataContext();
var emails = (from b in db.EmailAddresses
select new { b.EmailAddressID, b.BusinessEntityID }).Take(20);
topEmails.DataTextField = "BusinessEntityID";
topEmails.DataValueField = "BusinessEntityID";
topEmails.DataSource = emails;
topEmails.DataBind();
Default.aspx.cs:
FormSuccessBID.InnerHtml = "Business Entity ID: " + Request.Form["topEmails"] + "";
Any ideas why this wouldn't be working?
Update:
Contact.aspx:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h2 style="text-align: center;">Contact Kyle</h2>
<form action="Default.aspx" method="post" data-transition="pop">
<div data-role="fieldcontain">
<label for="userFName">First Name:</label>
<input type="text" name="firstName" id="uFName">
</div>
<div data-role="fieldcontain">
<label for="userLName">Last Name :</label>
<input type="text" name="lastName" id="uLName">
</div>
<div data-role="fieldcontain">
<label for="productsCategories">Products:</label>
<select name="productCategories" id="productCategories" data-native-menu="false" runat="server"></select>
</div>
<div data-role="fieldcontain">
<label for="topEmails">Business Entity ID:</label>
<select name="topEmails" id="topEmails" data-native-menu="false" runat="server"></select>
</div>
<input type="submit" data-iconpos="right" data-inline="true" data-icon="plus" name="sendMessage" id="sendMessage" value="Send Info">
</form>
</asp:Content>
You're missing "runat='server'" in your form definition.
In ASP.NET you're also allowed one form per page. Not sure from what you're posted if you're trying to put multiple forms on there, but if you are, it's not going to work.
I think all you need to do to accomplish what you want is have an ASP.NET button on the page, with the postbackURL set to the page you want to go to. (Of course, you need to do this in a server-side form)
I think you're mixing ASP.NET methods with other, different technologies. (From the look of it, you probably used classic ASP or PHP perhaps?)
If you can't use .NET for whatever reason, the ID/name of the field is not going to be what you're expecting. You need to inspect the form post and find its value - something along the lines of "clt00_somethingelse_topEmails". (Again, this is going to be a heck of a lot easier if you use the .NET way of doing things, but you may have a requirement not to)

Form Submit in my code does not work on webbrowser control C#

This a html code in site that i want to use it in my web browser control C# :
<form name="frmWorkBookPrintTerm" action="WorkBookPrintTerm.asp" target="_blank" method="post">
<hr width="90%">
<input type="Hidden" name="strTermCode" value="912">
<input type="Hidden" name="strStudentCode" value="896325605">
<input type="Button" onclick="javascript:document.frmWorkBookPrintTerm.submit();" value="...">
<hr width="90%">
</form>
But It is not working. When i want to go ".../WorkBookPrintTerm.asp" directly website says you did illegal job and otherwise web browser control is not working.
Thank You.

Linq to xml - access deep element

In C# i need to access element that is nested in many other elements. I could write a query by naming all of elements but that would be very time consuming. Is there any easy way to do this or maybe some generator?
for example i have a xml:
<div id="list_offers">
<div class="container">
<div id="header">
<div class="row">
<div class="space-on-sides">
<div class="sixteen columns relative">
<div class="logo">
<a href="/">
<img src="/images/design_resp/design/logo_profesia.png" width="113" height="76" alt="PROFESIA.SK - práca, zamestnanie, ponuky práce, brigáda, voľné pracovné miesto" title="PROFESIA.SK - práca, zamestnanie, ponuky práce, brigáda, voľné pracovné miesto" />
</a>
</div>
<div class="right-panel">
<a class="login-company small red button nice radius right hide-on-phones margin-on-left" title="Vstup pre firmy" href="/login_person.php?action=company_login">Vstup pre firmy</a>
<a class="login-company small red button nice radius right show-on-phones margin-on-left" href="/login_person.php?action=company_login">Firmy</a>
<a rel="nofollow" id="login_modal" class="small gray button nice radius right" title="Prihláste sa do konta" href="/login_person.php?action=login">Prihlásiť</a>
<div class="search">
<div class="relative hide-on-phones">
<form name="fulltextsearch" id="fulltextsearch" action="/search.php" method="get" onsubmit="if(document.getElementById('search').value=='HÄľadanie') return false; return true;" class="nice" />
<input name="which_form" type="hidden" value="simple" />
<input name="tab_name" type="hidden" />
<input name="search_anywhere" type="text" id="search" placeholder="HÄľadanie" tabindex="1" class="input-text expand" />
<input name="submit_search_simple" type="image" src="/images/design_resp/design/blank.png" alt="HÄľadaĹĄ" class="ico search-nosprite" /></form>
</div>
<div class="show-on-phones">
<a title="HÄľadajte ponuky práce podÄľa rĂ´znych kritĂ©riĂ">
</a>
</div>
</div>
<div class="row-header-favorite">
<div class="header-favorite">
<a title="Moje vybrané pracovné ponuky" class="ico-shopping-cart-toppanel" rel="nofollow" href="/user_details.php?action=show_my_offers&ref_top_panel=157">0</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
and i need access:
input name="tab_name" type="hidden"
You can use Linq to xml
var xDoc = XDocument.Load(pathToXmlFile); //XDocument.Parse(xmlstring);
var input = xDoc.Descendants("input")
.First(i=>(string)i.Attribute("name")=="tab_name");
XPath can be used too
var input = xDoc.XPathSelectElement("//input[#name='tab_name']");
If you need to access that element on server side, you'll need to add a runat attribute there (like this: runat="server"), to make it a server side control. I would strongly suggest making it a control of your page, then, so you can access it like any other variable. You would have to give it an ID, so at the very least you could fetch it through that property.
But! your problem seems to be more client side related, so I strongly, really strongly suggest you use jQuery instead. Get jQuery in your site, and it becomes as easy as:
$("input[name='tab_name']");
Or better yet, if you give it an ID:
$("#idYouGaveTotheField");

Categories

Resources