Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

So how I retrieve actual form data #1

Closed
mhesabi opened this issue Feb 21, 2015 · 3 comments
Closed

So how I retrieve actual form data #1

mhesabi opened this issue Feb 21, 2015 · 3 comments

Comments

@mhesabi
Copy link

mhesabi commented Feb 21, 2015

I'm using ASP.Net MVC after submitting form I only get key. how can I retrieve form data that submitted by user?

http://stackoverflow.com/questions/28654590/jcryption-asp-net-mvc

@JakeJP
Copy link
Owner

JakeJP commented Feb 24, 2015

There are some difficulties to use jCryption.NET with MVC. The biggest problem is that parameters that are passed from MVC model binder don't see the original Request.Form object. To get a model parameter filled properly, some trick is needed.
For the moment, quick but limited solution is like this:
Add code in your project

using System;
using System.Web.Mvc;
namespace jCryption
{
    public class jCryptionModelAttribute : CustomModelBinderAttribute
    {
        public override IModelBinder GetBinder()
        {
            return new jCryptionModelBinder();
        }
        public class jCryptionModelBinder : DefaultModelBinder
        {
            override public Object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                jCryption.HandleRequest(controllerContext.HttpContext.Request);
                bindingContext.ValueProvider = new FormValueProvider(controllerContext);
                return base.BindModel(controllerContext, bindingContext);
            }
        }
    }

    public class jCryptionHandshakeAttribute : ActionFilterAttribute
    {
        override public void OnActionExecuting( ActionExecutingContext filterContext)
        {
            jCryption.HandleRequest(filterContext.HttpContext.Request);
        }
    }
}

in the control class, declare an action to handle jCryption negotiation and add [jCryptionModel] attribute to LoginModel. ( this one does not work with AntiForgeyAttribute )

    using jCryption;
    public class AccountController : Controller
    {
        [jCryptionHandshake]
        [AllowAnonymous]
        public void jCryptionHandshake()
        {
        }

        [HttpPost]
        [AllowAnonymous]
        //[ValidateAntiForgeryToken]
        public ActionResult Login([jCryptionModel] LoginModel model, string returnUrl)
        {

in the view, write script to access the handshake action which is defined above.

    @jCryption.RenderScriptFor(null, src:"/Scripts/jquery.jcryption.3.1.0.js")
    <script>
          $(document).ready(function () {
            $('form').jCryption({
                getKeysURL: '/Account/jCryptionHandshake?getPublicKey=true',
                handshakeURL: '/Account/jCryptionHandshake?handshake=true'
            });
        });

    </script>

I'd better work hard to find an elegant solution of jCryption for MVC...

@JakeJP
Copy link
Owner

JakeJP commented Feb 25, 2015

Hi again. I revised this solution a bit easier.

All you need is:

using System;
using System.Web.Mvc;
namespace jCryption
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class jCryptionHandlerAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            jCryption.HandleRequest(filterContext.HttpContext.Request);
        }
    }
}

in controller

        // GET: /Account/Login
        [AllowAnonymous]
        [jCryptionHandler]
        public ActionResult Login(string returnUrl)
        {
             ....
        }

        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [jCryptionHandler]
        [ValidateAntiForgeryToken(Order=0)] // to make sure this action comes after jCryptionHandler
        public ActionResult Login(LoginModel model, string returnUrl)
        {

in view

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @jCryption.RenderScriptFor("form", src:"/Scripts/jquery.jcryption.3.1.0.mod.js")
}

A point is you need to use my "mod" version of jcryption.js which does not depend on Session key store. (since Session is disabled in MVC controllers by default)

@mhesabi
Copy link
Author

mhesabi commented Feb 25, 2015

Thank you, brilliant solution and support indeed. Thank you Jake so much.
in case anyone encountered javascript error at this line: $.jCryption.crypt = new JSEncrypt();
you need to add this : var $ = jQuery.noConflict();

@section Scripts {
    var $ = jQuery.noConflict();
    @Scripts.Render("~/bundles/jqueryval")
    @jCryption.RenderScriptFor("form", src:"/Scripts/jquery.jcryption.3.1.0.mod.js")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants