am trying to create a web page to be used by my network users (inside my intranet)

I want to get the visitor's name of my page ( in another words to get the network login user name of the person who is visiting my web page)

i tried to use the following code but am getting error

<%
' Use the Page.User property to access authentication and
' identity information for the current user. If the user is
' not authenticated, redirect them to a login page.
Sub Page_Load(sender As Object , e As EventArgs)
If User.Identity.IsAuthenticated Then
msgLabel1.Text = "Welcome, <B>" + User.Identity.Name + "</B>!"
Else
Response.Write "sorry"
End If
End Sub
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form runat="server">
<h3><asp:label id="msgLabel1" runat="server" /></h3>
</form>
</body>
</html>


but am getting the following error

Server Error in '/' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.
Source Error:

Line 3: ' identity information for the current user. If the user is
Line 4: ' not authenticated, redirect them to a login page.
Line 5: Sub Page_Load(sender As Object , e As EventArgs)
Line 6: If User.Identity.IsAuthenticated Then
Line 7: msgLabel1.Text = "Welcome, <B>" + User.Identity.Name + "</B>!"

Source File: c:\inetpub\wwwroot\f\username.aspx Line: 5

Show Detailed Compiler Output:

C:\WINDOWS\system32> "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc.exe" /t:library /utf8output /R:"C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll" /R:"C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /out:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\26676eb7\92c7e946\App_Web_szxvhp9l.dll" /debug- /win32resource:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\26676eb7\92c7e946\szxvhp9l.res" /define:_MYTYPE=\"Web\" /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Specialized,System.Configuration,System.Text,System.Text.RegularExpressions,System.Web,System.Web.Caching,System.Web.SessionState,System.Web.Security,System.Web.Profile,System.Web.UI,System.Web.UI.WebControls,System.Web.UI.WebControls.WebParts,System.Web.UI.HtmlControls "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\26676eb7\92c7e946\App_Web_szxvhp9l.0.vb" "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\26676eb7\92c7e946\App_Web_szxvhp9l.1.vb"

Microsoft (R) Visual Basic Compiler version 8.0.50727.42
for Microsoft (R) .NET Framework version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\26676eb7\92c7e946\App_Web_szxvhp9l.0.vb(101) : error BC30035: Syntax error.
Me.WriteUTF8ResourceString(__w, 0, 327, true)
~~
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\26676eb7\92c7e946\App_Web_szxvhp9l.0.vb(102) : error BC30429: 'End Sub' must be preceded by a matching 'Sub'.
End Sub
~~~~~~~
c:\inetpub\wwwroot\f\username.aspx(5) : error BC30289: Statement cannot appear within a method body. End of method assumed.
Sub Page_Load(sender As Object , e As EventArgs)
~~~
c:\inetpub\wwwroot\f\username.aspx(7) : error BC30451: Name 'msgLabel1' is not declared.
msgLabel1.Text = "Welcome, <B>" + User.Identity.Name + "</B>!"
~~~~~~~~~

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210

Here's the fix:

instead of the normal <% %> thing, switch to <script runat="server"></script>
Now that should solve the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.