Link to home
Start Free TrialLog in
Avatar of leskelly
leskellyFlag for United States of America

asked on

Web service in .NET project is initially unavailable unless anonymous access is granted.

I have an ASP.NET application developed in Visual Studio 2008 with VB.net and JavaScript.
In this app I have a master page with a link on it that when clicked calls a JavaScript function named YourAre().  This function calls a web service named HelpService that returns a string of HTML.

The problem is that if the user clicks on the link when first entering the app they will get an error message that says,
"Line: 54 Error: 'HelpService' is undefined"

If they navigate to another page or even refresh the page they don't get the error.  If I give anonymous access to the web service by putting the following in the web.config file the error not longer occurs.
       <location path="WebServices/HelpService.asmx">
            <system.web>
                  <authorization>
                        <allow users="?" />
                  </authorization>
            </system.web>
      </location>
 
This happens on both my development machine and the production machine.  This is an internal app and we use IE7 and 8, it happens with both.  I've attached the HTML and JavaScript for the master page and the VB for the web service.  Line 64 is the code for the link and line 112 is where the web service is called and the source of the error.  Any assistance would be greatly appreciated.
<%@ Master Language="VB" CodeFile="ClientTrack.master.vb" Inherits="ClientTrack" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Site Master Page</title>
    <asp:ContentPlaceHolder id="head" runat="server"></asp:ContentPlaceHolder>
   
    <link href="~/Styles/Master.css" rel="stylesheet" type="text/css" />
    <link href="~/Styles/Dialog.css" rel="stylesheet" type="text/css" />

    <script src="/WebApps/ClientTrack3Point5/js/jquery/jquery-1.4.2.js" type="text/javascript"></script>    
    <script src="/WebApps/ClientTrack3Point5/js/jquery/jquery-1.4.2-vsdoc.js" type="text/javascript"></script>
    <script src="/WebApps/ClientTrack3Point5/js/jquery/jQueryUI.js" type="text/javascript"></script>
    <script src="/WebApps/ClientTrack3Point5/js/Master.js" type="text/javascript"></script>
      
    <script type="text/javascript">
        window.onbeforeunload = confirmExit;
    </script>
    
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/WebServices/HelpService.asmx" />
           </Services>
        </asp:ScriptManager>
        <div id="divPageWrapper">
            <div id="divHeaderOne">
                <asp:Image runat="server" ID="HeaderImage" CssClass="HeaderOneImage" ImageUrl="~/Images/MasterHeader.gif"
                    AlternateText="Logo" />
            </div>
            <div id="divMenu" >
                <asp:Menu CssClass="MainMenu" ID="MasterMenu" runat="server" DataSourceID="SiteMapDataSource1"
                    Orientation="Horizontal" StaticEnableDefaultPopOutImage="False">
                    <StaticSelectedStyle CssClass="StaticSelectedStyle" />
                    <StaticMenuItemStyle CssClass="StaticMenuItemStyle" ItemSpacing="0px" />
                    <StaticHoverStyle CssClass="StaticHoverStyle" />
                    <DynamicMenuStyle CssClass="DynamicMenuStyle" />
                    <DynamicMenuItemStyle CssClass="DynamicMenuItemStyle" />
                    <DynamicHoverStyle CssClass="DynamicHoverStyle" />
                </asp:Menu>
            </div>
            <div id="divHeaderTwo">
                <asp:Label ID="lblFormTitle" runat="server" Text="FormTitle"></asp:Label>
            </div>
            <div id="divHeaderThree">
                <asp:UpdatePanel ID="UpdatePanelDDL" runat="server">
                    <ContentTemplate>
                        Records For
                        <asp:DropDownList ID="ddlRecordsFor" runat="server" Width="150px" CssClass="RecordsFor" AutoPostBack="True">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                        </asp:DropDownList>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
            <div id="divSideBar">
                <asp:LinkButton ID="LogOff" runat="server">Log Off</asp:LinkButton>
                <a id="SpellCheck" onclick="checkspell()">Spell Check</a><br />
                <a id="YouAre" onclick="YouAre()">You Are</a>
                <asp:HyperLink ID="YouCan" runat="server">You Can</asp:HyperLink>
                <asp:HyperLink ID="WhoCan" runat="server">Who Can</asp:HyperLink>
                <asp:HyperLink ID="HowCan" runat="server">How Can</asp:HyperLink>
                <asp:UpdateProgress ID="UpdateProgressButton" runat="server">
                    <ProgressTemplate>
                        <div class="Progress">
                        </div>
                        <br />
                        <div class="PleaseWait"> Please Wait...</div>
                    </ProgressTemplate>
                </asp:UpdateProgress>
            </div>
            <div id="divMainContent">
                <asp:ContentPlaceHolder ID="cp1" runat="server">
                    Under Construction
                </asp:ContentPlaceHolder>
            </div>
            <div id="divFooter">
                <asp:UpdatePanel ID="UpdatePanelButton" runat="server">
                    <ContentTemplate>
                        <asp:Button ID="btnDivideByZero" runat="server" Text="Divide by Zero" Width="125px" />
                        <asp:Button ID="btnPageNotFound" runat="server" Text="Page Not Found" Width="133px" />
                        <asp:Button ID="btnDatabaseError" runat="server" Text="Data base Error" Width="125px" />
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </div>
        <div id="divHelp" visible="false"></div>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />
    </form>
</body>
</html>


var Message = "";
var MessageTitle;
function YouAre() {

    if (MessageTitle == "You Are") 
    {
        $('div#divHelp').dialog("open");
    }
    else {
            $get("ctl00_UpdateProgressButton").style.display = "block";
            $get("ctl00_UpdateProgressButton").style.visibility = "visible";
            try
            {
                HelpService.YouAre(YouAreCallBack);
                var t = setTimeout("SetHelpDialog()", 2000);
            }
            catch(e)
            {
                if (e.description == "'HelpService' is undefined") 
                {
                    alert("'HelpService' is undefined");
                }
                else
                {
                    alert("Exception = " + e.number + " " + e.description);
                }
            }
    }
}

function YouAreCallBack(result) 
{
    if (result.substring(0,5) == "Error")
    {
        divHelp.title = result.substring(0, 5);
        MessageTitle = "Error";
    }
    else
    {
        divHelp.title = "You Are";
        MessageTitle = "You Are";
    }
        Message = result;
}

function SetHelpDialog() 
{
    divHelp.innerHTML = Message;
    
    $('div#divHelp').dialog({
        height: "auto",
        modal: true,
        overlay: { background: '#fff', opacity: '0.7'}
    });

    CenterDialog($('div#divHelp'));
    $get("ctl00_UpdateProgressButton").style.display = "none";
}

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<System.Web.Script.Services.ScriptService()> _
    <WebService(Namespace:="http://People-Inc.org/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class HelpService
    Inherits System.Web.Services.WebService

    <WebMethod(EnableSession:=True)> _
    Public Function YouAre() As String
        'Dim CurrentUser As New User("088316", "Group1|Group2")
        'Dim CurrentUser As New User("109537")
        'Dim CurrentUser As New User("065168")
        Dim CurrentUser As User = System.Web.HttpContext.Current.Session("CurrentUser")
        Try
            With CurrentUser
                Dim Title As String
                If .Title = "" Then
                    Title = "You have no title listed in Ultipro. To see more information on this topic click <a onclick=""DownLoadFile('http://Intranet/WebApps/ClientTrack3Point5/Help/ClientTrackManual.pdf#ProgInfGeneral')"">here.</a>"
                Else
                    Title = .Title
                End If

                Dim JobDescription As String
                If .JobDescription = "" Then
                    JobDescription = "You have no Job Description listed in Ultipro. To see more information on this topic click <a onclick=""DownLoadFile('http://Intranet/WebApps/ClientTrack3Point5/Help/ClientTrackManual.pdf#ProgInfGeneral')"">here.</a>"
                Else
                    JobDescription = .JobDescription
                End If

                Dim Supervisor As String
                If .Supervisor = "" Then
                    Supervisor = "You have no Supervisor listed in Ultipro."
                Else
                    Supervisor = .Supervisor
                End If

                Dim Programs As String
                If .Programs = "" Then
                    Programs = "There are no programs on the Program Information page for which you are listed as a staff. To see more information on this topic click <a onclick=""DownLoadFile('http://Intranet/WebApps/ClientTrack3Point5/Help/ClientTrackManual.pdf#ProgInfGeneral')"">here.</a>"

                Else
                    Programs = .Programs
                End If

                Dim Groups As String
                If .Groups = "" Then
                    Groups = "You are in no Active Directory Groups. To see more information on this topic click <a onclick=""DownLoadFile('http://Intranet/WebApps/ClientTrack3Point5/Help/ClientTrackManual.pdf#ProgInfGeneral')"">here.</a>"

                Else
                    Groups = .Groups
                End If
                Return String.Format("<u>YOU ARE:</u><br />{0} {1}<br /><br />" _
                                     & "<u>YOUR LOGIN NAME IS:</u><br />{2}<br /><br />" _
                                     & "<u>YOUR TITLE IS:</u><br />" & Title & "<br /><br />" _
                                     & "<u>YOUR JOB DESCRIPTION IS:</u><br />" & JobDescription & "<br /><br />" _
                                     & "<u>YOUR SUPERVISOR IS:</u><br />" _
                                     & "(If incorrect click " & "<a onclick=""DownLoadFile('http://Intranet/WebApps/ClientTrack3Point5/Help/ClientTrackManual.pdf#ProgInfGeneral')"">here.</a>)<br />" _
                                     & "{5}<br /><br />" _
                                     & "<u>THE PROGRAM(S) YOU ARE A {6} FOR ARE:</u><br /><div >" & Programs & "</div><br />" _
                                     & "<u>THE GROUPS YOU ARE IN ARE:</u><br /><div >" & Groups & "</div><br /><br />", _
                                     .FirstName, .LastName, _
                                     .UserName, _
                                     .Title, _
                                     .JobDescription, _
                                     .Supervisor, _
                                     .Title.ToUpper, _
                                     .Programs, _
                                     .Groups)
            End With
        Catch ex As Exception
            Return "Error<br />The following error has occurred in the You Are function of the Help Service Web Service<br /><br />'" & ex.Message & "'<br /><br />Copy and paste this message into an e-mail to the IT help desk or call the IT help desk and read this exact message to them."
        End Try
    End Function

End Class

Open in new window

Avatar of leskelly
leskelly
Flag of United States of America image

ASKER

I've re posted this question and restated it to more clearly reflect the problem.
Ignore that last post as it was supposed to be for another question.
Avatar of puru1981
puru1981

I think there is no need of script manager to call a webservice.

please check if it helps:

http://www.codeproject.com/KB/XML/marcelo888RemoteScript01.aspx
ASKER CERTIFIED SOLUTION
Avatar of leskelly
leskelly
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial