Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Webmethod always Returns 401

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?

like image 635
ale Avatar asked Sep 01 '26 12:09

ale


1 Answers

  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>
    
like image 96
ale Avatar answered Sep 04 '26 02:09

ale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!