开发者

ASP.NET Webmethod always Returns 401

开发者 https://www.devze.com 2023-01-05 15:19 出处:网络
Getting sick with asp.net permission madness... This time, I Just cant AJAX-CALL any kind of webmethod or i just get:

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?


  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>
    


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+

0

精彩评论

暂无评论...
验证码 换一张
取 消