ASP.NET Webmethod always returns 401

Get rid of the frenzy of asp.net permissions ... This time I just can't use AJAX-CALL any web method, or I just get:

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

the 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();
                    }
                }
        });

SOME MISTAKES IN SUCCESS I KNOW. THIS IS NOT A PROBLEM NOW. Firebug Console always returns 401 Unauthorized when I try to make an ajax call.

Is anyone

+3
source share
2 answers
  • Webmethod Should be generic (vb) / static (C #), if you get ERROR 500, mark your method as Shared / Static and you're done.

  • Error 401: If you use forms authentication, remember to allow anonymous access to your login page by doing this on your website:

    <location path="Login.aspx">
      <system.web>
        <authorization>
          <allow users="*"/>
        </authorization>
      </system.web>
    </location>
    
+10

.

1) web.config

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

2) ~/App_Start/RouteConfig.cs ,

: settings.AutoRedirectMode = RedirectMode.Permanent;

: settings.AutoRedirectMode = RedirectMode.Off;

3) ajax resolurl, : 'userForm.aspx/getAllUsers'

: '& lt;% = ResolveUrl ("userForm.aspx/getAllUsers")%>'

, jQuery :

  $.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);
    }
  })

, .https://forums.asp.net/t/1975897.aspx?jquery+ajax+calls+to+asp+net+web+methods+authentication+error+

0

Source: https://habr.com/ru/post/1753224/


All Articles