VB.Net web method giving HTTP401 Not Authorized error

I use web methods in several places on the int intranet site that I am working on and they work fine, however one page constantly throws an HTTP 401 error every time I try to access the web method.

I'm not sure how to approach this problem, I tried to comment everything from the web method and still get error 401, even the basic connection to the database, just doing SELECT 1 not displayed when I look at the DB with the profiler.

My web.config is the same for all pages on the intranet, and I do not see any differences in my ASP.Net page compared to pages that use web methods.

Can someone tell me why this can happen only for this page and not for others? And also, how can I get around this problem?

ASP.Net Code (called from the OnClientClick button)

  function SendEmails() { var Grid = document.getElementById("instructorGrid"); var mailBody = document.getElementById("txtMailBody"); var ddlDutyPeriod = document.getElementById("DDL_DutyPeriods"); var cell = Grid.rows[i].cells; var HTML = cell[0].innerHTML; var chkBox = cell[5].innerHTML; PageMethods.TestMethod() } 

Script manager

  <asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true" EnablePageMethods="true" EnableScriptLocalization="true"> </asp:ScriptManager> 

Code VB.Net

  <System.Web.Services.WebMethod()> Public Shared Sub TestMethod() 'Dim conn1 As New SqlConnection(ConfigurationManager.ConnectionStrings("Blueprint").ToString()) 'Dim cmd2 As New SqlCommand 'cmd2.CommandType = CommandType.Text 'cmd2.CommandText = "SELECT 1" 'cmd2.Connection = conn1 'conn1.Open() 'cmd2.ExecuteNonQuery() 'conn1.Close() End Sub 

Fiddler Results

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> <HTML><HEAD><TITLE>Not Authorized</TITLE> <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD> <BODY><h2>Not Authorized</h2> <hr><p>HTTP Error 401. The requested resource requires user authentication.</p> </BODY></HTML> 

UPDATE:

I tried to use user impersonation in code like half in this link http://support.microsoft.com/kb/306158 , however I can’t do this because the method is a web method.

I also tried adding anonymous access to my login page in web.config as suggested below, but that also did not help

+6
source share
3 answers

I think you are using FormsAuthentication. You must provide anonymous access to your account.

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

Add the impersonation tag to the web.config file. This should allow you to impersonate a specific account when trying to access your web method. See this support article for directions.

 <system.web> <identity impersonate="true" userName="Yang" password="bar" /> </system.web> 
+2
source

This is a long shot, but do you have it in your Web.Config?

 <system.web.extensions> <scripting> <webServices> <authenticationService enabled="true" /> </webServices> </scripting> </system.web.extensions> 
+1
source

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


All Articles