5

Getting sick with asp.net permission madness... This time, I Just cant AJAX-CALL any kind of webmethod or i just get:

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

Code:

<WebMethod(True)> _
    Public Function Login(ByVal usuario As String, ByVal senha As String) As Boolean
        [lots of validations]
        If (con.Connection.State = ConnectionState.Open) Then
            Return True
        Else
            Return False
        End If
    End Function

JQUERY CALL:

$("#btnEnviar").click(function() {
            $('#login').hide();
            $('#ajaxLoader').fadeIn();
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Login.aspx/Login",
                data: "{'usuario':'" + $('#txtUsuario').val() + "','senha':'" + $('#txtSenha').val() + "'}",
                dataType: "json",
                dataFilter: function(data) {
                    var msg = eval('(' + data + ')');
                    if (msg.hasOwnProperty('d'))
                        return msg.d;
                    else
                        return msg;
                },
                success: function(msg) {
                    if (msg.UsuarioValido == '1') {
                        top.location = "Home.aspx"
                    }
                    else {
                        $('#ajaxLoader').hide();
                        $('#login').fadeIn();
                    }
                }
        });

THERE ARE SOME MISTAKES ON THE SUCCESS STUFF I KNOW. THATS NOT THE PROBLEM FOR NOW. Firebug Console always return 401 Unauthorized when i Try an ajax call.

Anyone?

3
  • what auth have you got setup in the web config?
    – redsquare
    Jul 5, 2010 at 23:32
  • <authentication mode="Forms"> <forms name="Login" path="/" loginUrl="Login.aspx" protection="All" timeout="30" defaultUrl="Home.aspx"> </forms> </authentication>
    – ale
    Jul 5, 2010 at 23:33
  • I assume you issue the authentication key to the client where you have the [lots of validations] placeholder?
    – redsquare
    Jul 5, 2010 at 23:42

2 Answers 2

12
  1. Webmethod Should be shared (vb) / static (C#) if you are getting ERROR 500, mark your method as Shared/Static and you are done.

  2. About Error 401: If you are using forms authentication remember to allow anonymous access to your login page by doing this on your web.config:

    <location path="Login.aspx">
      <system.web>
        <authorization>
          <allow users="*"/>
        </authorization>
      </system.web>
    </location>
    
1
  • I forgot the 'static' keyword and got 401 that I didn't understand.. Thank you Aug 17, 2021 at 11:05
1

These steps fixed the problem for me.

1) Add the below code in web.config

  <system.web>
   <authorization>
     <allow users="*"/>
   </authorization>
 </system.web>

2) In ~/App_Start/RouteConfig.cs, change as below

From: settings.AutoRedirectMode =RedirectMode.Permanent;

To: settings.AutoRedirectMode = RedirectMode.Off;

3) In the ajax call, use the resolveurl method, From: 'userForm.aspx/getAllUsers'

To: '<%= ResolveUrl("userForm.aspx/getAllUsers") %>'

So that the final jQuery code looks like this:

  $.ajax({
    url: '<%= ResolveUrl("userForm.aspx/getAllUsers") %>',
    async: false,
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
      source.localdata = data.d;
    },
    error: function (err) {
      alert('Error: ' + err);
    }
  })

Please refer the below link for more information. https://forums.asp.net/t/1975897.aspx?jquery+ajax+calls+to+asp+net+web+methods+authentication+error+

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.