Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 197 Vote(s) - 3.72 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"

#1
Been stuck with this for hours

<!-- language: lang-json-->

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

I'm trying to call this WebMethod in my ASP.Net Webform
<!-- language: cs-->

[WebMethod]
public static string GetClients(string searchTerm, int pageIndex)
{
string query = "[GetClients_Pager]";
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
return GetData(cmd, pageIndex).GetXml();
}

From this jquery.ajax
<!-- language: javascript -->

function GetClients(pageIndex) {
$.ajax({
type: "POST",
url: "ConsultaPedidos.aspx/GetClients",
data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
}

But I always get this error:

> POST `http://localhost:64365/ConsultaPedidos.aspx/GetClients` **401
> (Unauthorized)**

Weird thing is that this used to work until I start authenticating users

<!-- language: xml-->

<system.web>
...
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="/Dashboard" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
...
</system.web>

Any ideas?
Reply

#2
Not an expert but have you tried by putting `<allow users="*"/>` in the config file?
Your request should be using a GET method and not a POST (used to create).

EDIT: It seems that you are using a SOAP method, which can't be called from clientside, you should use a RESFUL service.
Reply

#3
## Problem solved ##
This was driving me crazy.

Inside **~/App_Start/RouteConfig.cs** change:

<!-- language: cs-->

settings.AutoRedirectMode = RedirectMode.Permanent;
To:
<!-- language: cs-->

settings.AutoRedirectMode = RedirectMode.Off;

(Or just comment the line)

Also if friendly URLs are enabled you need to change
<!-- language: js-->

url: "ConsultaPedidos.aspx/GetClients",
To:
<!-- language: js-->

url: '<%= ResolveUrl("ConsultaPedidos.aspx/GetClients") %>',


Hope this help somebody else
Reply

#4
I faced the same problem and firstly tried the solution which is available and it does work. However I realised if I create a new `web-service` and add the relevent code in the `App_Code` folder of the `web-service` file then I don't get any errors.
Reply

#5
401 Unauthorised means that:

- User authentication hasn't been provided *or*
- It was provided but failed authentication tests

This corroborates with what you've said about adding authentication, it's clearly covering this method too.

Therefore do you want access to this method to be public or not?

**Public**:

- You need to remove authentication from this method.

To allow access to public resources (such as this webmethod) you simply place this in the config file in the same directory:

<authorization>
<allow users="*" />
</authorization>

if you put above tag then it will give access right to all kind of users to all resources. so instead of that you can add below tag to give authorization to the web service

<location path="YourWebServiceName.asmx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>

**Private**:

- You need to ensure authentication is being sent across the line (using [Fiddler][1] to check for the cookie), and ensure it's is passing asp.net authentication.


[1]:

[To see links please register here]

Reply

#6
I know you got your answer accepted. It actually happens while creating a Web Form application and not changing the `Authentication` to `No Authentication`.

The default authentication we see as `Authentication: Individual User Account`

The error will not come if we change to `Authentication: No Authentication`
Reply

#7
Inside `~/App_Start/RouteConfig.cs` change

settings.AutoRedirectMode = RedirectMode.Permanent;

to

settings.AutoRedirectMode = RedirectMode.Off;
Reply

#8
I found this question trying to solve the same problem, the way I solved is not seen here so I post to someone stuck in the same bottleneck:

Try in your method use the `EnableSession = true` in your WebMethod attribute

like

[WebMethod(EnableSession = true)]
public static string MyMethod()
{
return "no me rindo hdp";
}

with this is solved my 401 error. Hope be helpful.
Reply

#9
My site was using ASP.NET forms authentication and I worked out by trial and error that I could only get it work if I called a web method with on an `.asmx` page and with `contentType: "application/x-www-form-urlencoded"` and `dataType: "xml"`
Reply

#10
In my case, I was adding the WebMethod to a control on the form. It needs to be added to the page itself.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through