Install ReCaptcha to Umbraco V7 - c#

I would like to install ReCaptcha to our Umbraco Version 7. But I can't seem to find the correct approach. Basically I want to add the captcha element inside my custom form (not the Umbraco Form). Is there a way to do it? It seems the approach of adding the Recaptcha is not the same as how you add it in PHP application. How should I do this in Umbraco?
Update:
Recaptcha version can either be version 1, 2 or 3

reCaptcha V2 only requires a few simple lines of HTML to be inserted, AFAIK. You should be able to insert both a script tag and an HTML element anywhere inside your form element (inside Html.BeginForm if that's what you're doing) in your custom form, as long as you have an API key. I did this the other day and it just worked.
https://developers.google.com/recaptcha/docs/display
It doesn't need "installation", but it requires some fiddling with code.

I wrote a post about it here a while back, https://www.jondjones.com/learn-umbraco-cms/umbraco-7-tutorials/umbraco-security/how-to-add-a-recapture-within-your-umbraco-website/
The quick option is to install recaptha mvc via Nuget and then decorate your controller with CaptchaValidator and use the Recaptcha in your HTML

In your controller just check the form values and get the captcha value from it like this below, if that value is null then the person hasn't filled it otherwise it will have a value in it.
var formData = Request.Form;
var captchaRequest = formData["g-recaptcha-response"];
if (string.IsNullOrWhiteSpace(captchaRequest))
{
TempData["formError"] = "Fill in the Captcha box.";
return CurrentUmbracoPage();
}

Ok I found a solution with detailed explanation in this Blog

Related

Blazor Server download file with parameters

I have a Blazor Server app where the users can use filters to see a list of movies. For example, they can specify the year, the country and etc. and movies that match the criteria will be displayed on the page (a list of Movie objects).
I want to give users the ability to save the displayed list as a text file. I know how to make an HTTP Get request to another Razor page or an MVC or API controller to return a predefined file but am struggling with passing any arguments to the endpoint.
Initially I thought of using HttpClient.PostAsJsonAsync to send a POST request to an API controller, passing the list as the request body, which works fine in the sense that if I put a breakpoint in the controller method it is hit but nothing is returned to the user.
If I use an <a> element or a button onClick method with NavigationManager.NavigateTo it works fine for a predetermined file HTTP Get request but how can I serve the users with a file that consists of the list of movies/objects they are seeing on the browser?
I.e. How can I either pass arguments with NavigationManager.NavigateTo or using an <a> element or any other way in order to send data to the endpoint/server which it then can use to generate a file and return that file to the user for saving?
Mock code to give an idea of what I'd like. This is just a design/idea reference, doesn't have to be WebApi, MVC or whatever. I just need something that would work with Blazor:
[HttpPost]
File WebApiMethodThatDoesTheWork(CustomObject data)
{
StringBuilder stringBuilder = new();
foreach (var item in data.Items)
{
stringBuilder.Append(item);
}
File.WriteAllText("test.txt", stringBuilder.ToString());
return File("test.txt");
}
The only two requirements I have about how the file will be downloaded are: it should be user-friendly, meaning the usual "Do you want to open or save this file?" dialog box would be perfect AND it shouldn't use the GET request to a Razor page to retrieve the file UNLESS a single Razor page can be used to download many different file types. I.e. if a user wants to download a list of movies, while another a list of songs. With my current knowledge I think for such a case I'd have to have 2 different Razor pages, which is suboptimal.
I don't have a complete packaged up answer because mine is old and Blazor works differently now, but the underlying principal is the same.
Step 1 - JS Interop call to URL.createObjectURL(new Blob([{your data}]))
Step 2 - Render an anchor tag with the resulting object url as the href and a download attribute.
<a id="download" href=#BlobUrl download=#FileName>Download File</a>
Step 3 - Optionally JS Interop call to click the rendered anchor tag so the user doesn't have to.
Whether the user sees a Save As.. dialog or not depends on their browser/operating system - you can't control that (There is a downloads api that has saveas functionality, but it's not fully supported)
You could try re-filtering the list using that file and upon start-up you can check if they have that file... there's lots of options you can chose from

How do access the tab details in C# from Umbraco 7?

Please see following image.
Umbraco7 screen
I am using Umbraco 7. You can see in the image that I have 'General Messages' tab.
I have saved all error messages in that and i need to access these error messages from the code, can I do that in Csharp ?
I'm going to assume that you have a template assigned to the sign up page, and that you want to get at the messages on that View.
That being the case, you can use either:
#Umbraco.Field("yourPropertyAliasHere")
or
#Model.Content.GetPropertyValue("yourPropertyAliasHere")
The main difference is that Umbraco.Field has a bunch of useful additional parameters for things like recursive lookup.
If you want get at the properties from within some random C# in your Umbraco site that isn't related t the actual signup page, assuming you have an Umbraco Helper, you can do the following:
var page = Umbraco.TypedContent(1234); //replace '1234' with the id of your signup page!
var message = page.GetPropertyValue<string>("yourPropertyAliasHere");

Redirecting webpage in ASP.NET depending on the URL

I've a domain name called mywebsite.com but I prefer users to access to my website through the www subdomain.
How can I achieve a verification and redirection in asp.net mvc3 easily?
When I was using php I did something like that :
if($_SERVER['SERVER_NAME'] != "www.mywebsite.com")
header('Location: www.mywebsite.com');
I'd like to find a similar way to achieve this kind of redirection in asp.net (C#) (I'd prefer not to set a 301 redirection but just a soft redirection like the one I was using in PHP).
I know I could do a verification in each of my controllers action methods and the redirect the user if he is not on the subdomain www.mywebsite.com but I'd prefer to write once the verification and the redirection and I can't find where :/
Thanks a lot !
You could use:
Request.Url.ToString()
This will return the URL , then you can quickly check if it contains 'www.' and redirect if true. However I would suggest that your handle this in the URL rewrite in IIS.
Follow the guide here: http://www.dotnetexpertguide.com/2011/08/iis-7-redirect-domaincom-to.html this is for making domain.com go to www.domain.com, but it works the same way for the opposite.
you could try something like this
if(!Request.Url.Host.Equals("www.mywebsite.com"))
{
Response.Redirect("website");
}
If you are looking to just add/append header information, then use Response.AppendHeader().
The Request.Url.ToString() property can be checked for the url you are looking for as per Ryan's answer.
Actually, the original question asks specifically for a 'soft' redirection (201 or 202) instead of a Moved Permanently/Found (301/302), so i think that instead of Response.Redirect the conditional line should be:
if(!Request.Url.Host.Equals("www.mywebsite.com"))
{
HttpContext.Current.Response.Headers.Add("Location", "www.mywebsite.com");
}
EDIT: I believe the status may also be set directly, used in conjunction with Response.Redirect:
if(!Request.Url.Host.Equals("www.mywebsite.com"))
{
HttpContext.Current.Response.Redirect("www.mywebsite.com");
HttpContext.Current.Response.Status = "201 Created";
}
If I am not totally missing something here, can't you just setup the host-header in the IIS settings for the site to include both www.domain.com as well as domain.com?

route.MapPageRoute to a page with an extension

In the global.asax of my website project (not MVC, not Web Application) MapPageRoute won't let me map to a page with an extension.
For example:
routes.MapPageRoute("GetMobilePassForAttendee", "Pass/Attendee/{attendeeId}", "~/GetMobilePass.aspx");
works as expected, but
routes.MapPageRoute("GetMobilePassForAttendee", "Pass/Attendee/{attendeeId}/pass.pkpass", "~/GetMobilePass.aspx");
returns a 404.
Anyone know why?
Perhaps I should be using URL rewriting, but all the material I've read has suggested I use routing instead.
Have you tested the "pass.pkpass" page separately, let say attendeeid=1 to see if Pass/Attendee/1/pass.pkpass is a working page without the routing?
Because .NET routing would not work in this case if this is a working page. It will skip the routing and load the actual "Pass/Attendee/1/pass.pkpass" page.
This worked for me:
routes.MapPageRoute("FederationMetadataRoute", "FederationMetadata/2007-06/{file}", "~/FederationMetadata/2007-06/FederationMetadata.aspx")
And then I just check the {file} parameter to make sure it equaled FederationMetadata.xml
So in your case, just set Pass/Attendee/{attendeeId}/pass.pkpass to Pass/Attendee/{attendeeId}/{pkpass}
and check {pkpass} to make sure it equals pass.pkpass on your page.

ASP.NET MVC Client-side validation with MvcContrib FluentHtml

What's the recommended way to do client-side validation using the built-in MVC2 code with MvcContrib's FluentHtml builders? We're using the jQuery client-side validation code, not the default Microsoft AJAX stuff, if that matters (though I don't think it should).
It seems the client-side validation only gets registered with jQuery Validate when you place a validation message (Html.ValidationMessageFor(x => x.FirstName)) on the page. MvcContrib's FluentHtml this.ValidationMessage(x => x.FirstName) only works with ModelState on the server side, doesn't write out any HTML if there's no error, and doesn't register the given property with jQuery Validate on the client-side.
So my question: is there a way to make the current trunk build of MvContrib work with MVC2's built-in client-side validation somewhat painlessly right now? If so, how? If not, is there another client-side validation that's recommended (other than xVal, which we're currently using and has been depreciated)? Should this be patched in MvcContrib so it works properly? A last resort would be to move to using ASP.NET MVC's built-in input builders, but we already invested a lot in MvcContrib's and would rather not.
Thanks!
Im in the exact same situation...i came across this post with in interesting comment further down although I couldn't quite get it to work.
http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/
If you can make any sense of it would be good to post it back up here.
Paul
I got the comment from that blog article working Paul, and modified it to use all the known MVC validation adapters instead of just the Required one (basically mimicking much of what's in the framework itself). It gets kind of hairy with how it displays the error message and working with what we already have, and I implemented a patch for MVC Contrib to work with it, but in the end I'm giving up for now until MVC3 is finialized and MVC Contrib builds against it. No point in going through all this when there's an updated release coming soon.
Here's what I ended up with (FluentViewPage<T> is where we add behaviors):
public class ClientsideValidationBehavior<T> : IBehavior<IMemberElement> where T : class
{
private readonly FluentViewPage<T> _viewPage;
public ClientsideValidationBehavior(FluentViewPage<T> viewPage)
{
_viewPage = viewPage;
}
public void Execute(IMemberElement element)
{
var attribute = element.GetAttribute<ValidationAttribute>();
if (attribute == null)
{
return;
}
var formContext = _viewPage.ViewContext.FormContext;
var fieldMetadata = formContext.GetValidationMetadataForField(UiNameHelper.BuildNameFrom(element.ForMember), true);
var modelMetadata = ModelMetadata.FromStringExpression(element.ForMember.Member.Name, _viewPage.ViewData);
var validators = ModelValidatorProviders.Providers.GetValidators(modelMetadata, _viewPage.ViewContext);
validators.SelectMany(v => v.GetClientValidationRules()).ForEach(fieldMetadata.ValidationRules.Add);
fieldMetadata.ValidationMessageId = element.ForMember.Member.Name + "_Label";
}
}
Hope that helps some.

Categories

Resources