MVC 5 encrypt parameters–SEO

 

I have added SEO ( ok, a bullshit example of SEO ) to http://msprogrammer.serviciipeweb.ro/2017/03/20/mvc-5-encrypt-parameters/ .

This is pretty simple :

 public class EncDecSEOBullShit : IEncryptDecrypt
    {
        //public static string FullName = typeof(EncDecSEOBullShit).AssemblyQualifiedName;
        const string seo = "this-is-seo-";
        public string DecryptString(string value)
        {
            return value.Replace(seo, "");
        }

        public string EncryptString(string value)
        {
            return seo + value;
        }
    }

 

the action is

<a href=’@Url.ActionEnc(new EncDecSEOBullShit(), “TestEncryptSEO”, new {id=7, a = 1, b = “asd” })’>Test</a>

It generates url such as:

http://mvc5encrypt.apphb.com/Home/TestEncryptSEO/7?a=this-is-seo-1&b=this-is-seo-asd

The Action Filter to decrypt takes the full type name ( with assembly name) of the class that does SEO ( or encrypt / decrypt)

[MVCDecryptFilter(EncDecFullClassName = "MVCEncryptDemo.EncDecSEOBullShit, MVCEncryptDemo")]

Example at

http://mvc5encrypt.apphb.com/

MVC 5 encrypt parameters

 

This is an old idea that I was having. The problem was that , for many people, showing the parameters in MVC is not something that they want . For example. suppose that we have this action

 

public ActionResult TestEncrypt(int id, int a, string b)

 

The this action can be activated by putting in the Razor cshtml file this

 

<a href=’@Url.Action("TestEncrypt", new { id=7, a = 1, b = "asd" })’>Test</a>

 

that will generate this URL :

http://mvc5encrypt.apphb.com/Home/TestEncrypt/7?a=1&b=asd .

The parameters are passed in clear text ( a is 1 and b is asd). What if I want to encrypt those into the URL ,

http://mvc5encrypt.apphb.com/Home/TestEncrypt/7?a=EAAAAHT3XGpsOow%2f2Wto%2fho1C3Bmy1kTFnBosorsrt9X3Eqj&b=EAAAADbjm%2bS8NDAKqznGI%2bzF02oOAY9wf24SFyFxPxbCu0ea

 

but receive in the Action the default values ( 1 and asd)?

Enter MVC5Encrypt

What you have to do is modify slightly the code in the .cshtml and add an attribute to the Action

 

<a href=’@Url.ActionEnc("mySecret", "TestEncrypt", new { id=7, a = 1, b = "asd" })’>Test</a>

 

and the action will be

[MVCDecryptFilter(secret = &quot;mySecret&quot;)]   
public ActionResult TestEncrypt(int id, int a, string b)

 

You can see into action at

http://mvc5encrypt.apphb.com/Home/TestEncrypt/7?a=EAAAAHT3XGpsOow%2f2Wto%2fho1C3Bmy1kTFnBosorsrt9X3Eqj&b=EAAAADbjm%2bS8NDAKqznGI%2bzF02oOAY9wf24SFyFxPxbCu0ea

 

FAQ:

1.  What is “mysecret”?

See code on http://stackoverflow.com/questions/202011/encrypt-and-decrypt-a-string that I shameless copied.

 

2. What about backward compatibility ( i.e., old links will function ) ?

Yes if you do not already encode in base64 ( default class encrypter knows if the parameter value is in base64 ) . See

http://mvc5encrypt.apphb.com/Home/TestEncrypt/7?a=1&b=asd 

 

3. What about extending this with a custom encrypt class ?

You can – see this function

 

public static string ActionEnc(this UrlHelper helper, IEncryptDecrypt encDec, string actionName, object routeValues)

 

4. What about extending this to route parameters ( e.g. http://localhost/Person/Edit/5  – the 5 parameter is in the route and not encrypted ) ?

Glad you ask. Please fill an feature request on github

5. More details ? 

Sources on GitHub : https://github.com/ignatandrei/MVC5Encrypt

Demo at http://mvc5encrypt.apphb.com/ 

NuGet at https://www.nuget.org/packages/MVC5Encrypt/

( Other solution is to use http://madskristensen.net/post/httpmodule-for-query-string-encryption)

64 bit accessing 32 bit application error

 

Mixing 32 bit with 64 bit it is not allowed – see https://support.microsoft.com/en-us/kb/282423 .

However, there are several solutions to accesing 32 bit from 64 bit :

1. Simpler – create a console exe (32 bit) application that calls the library ( same bit version) . Use this from the main program.

Cons: Error handling , difficulty to debug

2. Complicated :Read this and make your solution https://blog.mattmags.com/2007/06/30/accessing-32-bit-dlls-from-64-bit-code/

3.Use a NUGET package to call 32 bit from 64 bit.

See https://github.com/CodefoundryDE/LegacyWrapper and details here: https://blog.codefoundry.de/programming/legacy-wrapper-invoking-an-unmanaged-32bit-library-out-of-a-64bit-process/

Programming Tools 2016

Those are my programming tools in 2016.  I do not put links- if you do not find them on google , please send me an email. Or comment here.

blog

Syntax Highliter
Windows live writer
WordPress

Communications

outlook
skype
Slack

Components

Angular
Asp.net mvc
Caliburn.Micro
Irony
Jquery
Jquery ui
jstree
knockoutjs
NPOI
Prism
Select 2
Selenium

Database

Elastic search
Mongodb
Sql server

NuGet

EntityFramework
ExporterWordExcelPDF
HtmlAgilityPack
iTextSharp-LGPL
MediatR
Microsoft.AspNet.WebApi.Client
nlog
NReco.VideoConverter
OneNoteOCR
structuremap
Swashbuckle.Core
T4MVC
Untiy
WebActivator
Westwind.RazorHosting
WinSCP
xunit

Presentations

powerpoint
reveal

ProgrammerUtilities

7zip
AutoHotkey
Excel
free commander
SysInternals ADSI
nugetpackage explorer
VS2015
WinMerge

RemoteUtilities

bginfo
keepass
terminals

Reporting

kibana
powerbi

ToDO Lists

coachme
Todoist
trello

VersionControl

git
mercurial
perforce

Five common mistakes for ASP.NET MVC accesing resources : css, js, images, ajax

This is a Razor /MVC5 variant of older post
http://msprogrammer.serviciipeweb.ro/2010/10/09/five-common-mistakes-for-asp-net-mvc-accesing-resources-css-js-images-ajax/

To have once for all the link to show to people, because too much makes the same error again and again. (From here – you can use ResolveUrl or Url.Content – it’s the same for me. I use ResolveUrl because I used first …)

Case 1 The image does not display

Please check you have the following :

 <img src="@Url.Content("~/Content/images/YOURIMAGE.jpg" )" alt="image" style="border:0" /> or 
 
<img src="~/Content/images/YOURIMAGE.jpg" alt="image" style="border:0" /> 

Case 2 The css does not show

Please check you have the following :

 @string.Format("<link href="{0}" type="text/css" rel="stylesheet" />", Url.Content("~/Content/your.css"))  

or

 <style type="text/css">
@import '@Url.Content("~/Content/your.css")';

</style>  
  <style type="text/css">
<link href='~/Content/your.css' type='text/css' rel='stylesheet' />

</style>  

Case 3 The js does not execute (undefined error)

3.1 Please check you have the following :

 <script type="text/javascript" src="@Url.Content("~/Scripts/yourjs.js")"></script> 

or

 <script type="text/javascript" src="~/Scripts/yourjs.js"></script> 

This should be either in _Layout.cshtml, or in the scripts section

 @section scripts {   } 

in your .cshtml file.
3.2 please check in browser console ( press F12 ) for any javascript error that code may have

Case 4 The js does execute in .cshtml page, but did not execute under js file

Please check you DO NOT have the following @something in the js file. The js file is not interpreted by the same engine as cshtml, so can not interpret asp.net tags. Instead , add a parameter to your function for each @ variable that you have. Simple example : Let’s say in cshtml you have :

 <script type=”text/javascript”>

function Generate(){

window.open('@Url.Action(“About”)’);
}

</script> 

and you call Generate() from .cshtml file. Now you want the same in a .js file. The function Generate will have a parameter instead of Razor variable

 function Generate(url){  window.open(url); } 

and call like this from .cshtml file:

 Generate('@Url.Action("About")'); 

Case 5 The ajax request gives you a 404 error.

Please ensure that you have

 @Url.Content("~/path”) 

and not

 /path 

when you call the Ajax URL

Bonus 1: T4MVC , http://mvccontrib.codeplex.com/releases

Bonus 2: Edit the project file and put <MvcBuildViews>true</MvcBuildViews>

( short url: http://bit.ly/asp5Mistakes)