0

I am working on asp.net web api. I am trying to make a call to controller(Security) using jquery ajax calls. I have a method in my controller with 3 parameters like,

public WebRequest GetRequest(string method,string type,string endpoint)
        {
        RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
        var request = WebRequest.Create(endpoint);
        request.Method = method;
        request.ContentType = type;
        UnicodeEncoding enc = new UnicodeEncoding();
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        request.Headers.Add("Authorization-Token", RSAClass.StringConverter(RSAClass.RSAEncrypt(enc.GetBytes("User1"), rsa.ExportParameters(false), false)));
        return request;
}

and i am making a jquery ajax call like,

CreateRequest("GET", "application/json;charset=utf-8", "http://localhost:49847/api/Security", function (request) { alert("Hello"); });  


function CreateRequest(method, type, endpoint, callback) {
        $.ajax({
            url: "api/Security",
            data: { method: method, type: type, endpoint: endpoint }, 
            type: "GET",
            contentType: "application/json;charset=utf-8",
            statusCode: {
                200: function (request) {
                    callback(request);
                }
            }
        });

and also i have customfilterattribute class to validate authorization token like,

public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            string token;
            try
            {
                token = actionContext.Request.Headers.GetValues("Authorization-Token").First();
            }
            catch
            {
                actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest) { Content = new StringContent("missing authorization token") };
                return;
            }
            try
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                UnicodeEncoding enc = new UnicodeEncoding();
                AuthUsersRepository.GetAllUsers().First(x => x.Name ==enc.GetString(RSAClass.RSADecrypt(RSAClass.ByteConverter(token), RSA.ExportParameters(true), false)));
                base.OnActionExecuting(actionContext);
            }
            catch
            {
                actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden) { Content = new StringContent("Unauthorized User") };
                return;
            }
        }

when iam making first request it asking me for authorization token. and also showing null values in the 3 parameters(method,type,endpoint). guide me.

2 Answers 2

2

You need this:

data: { 'method': method, 'type': type, 'endpoint': endpoint },

This passes the values in the request, although in your example I'm not sure why you would need to pass endpoint as a parameter to the method if its value is the URL of the controller / action?

Sign up to request clarification or add additional context in comments.

4 Comments

@Aliostad you can pass data in a GET or POST. The documentation states that the data will be added to the querystring for a GET. api.jquery.com/jQuery.ajax
Thanks. Can you please update your post so I can change my -1 to +1?
Just any update would do. SO does not allow me to change mu upvote downvote after a few minutes.
@Aliostad I've given it a go!
0

You either need to

1) change your GET to POST the data as JSON. GET cannot have content. 2) Pass the data as query string parameters

If I am to do it myself, I would go for case 2:

http://localhost:49847/api/Security?method=someMethod&type=someType&endpoint=someendpoint 

Since this is security related, it has to be HTTPS.

But I would not do it myself, instead use what security experts have developed: "Thinktecture.IdentityModel.40"

https://github.com/thinktecture/Thinktecture.IdentityModel.40

1 Comment

You don't need to convert to a POST or manually write the querystring yourself. Check the jQuery documentation: api.jquery.com/jQuery.ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.