I am trying to run a script when a button is pressed on my Login.aspx page in Visual Studio 2008. I have set the Sub routine which is located within the script to redirect me to a certain page when the script is run for testing purposes. When I press the button the script is not executed and I am redirected to my default.aspx page instead of the one specified. Can someone help me I am new to asp.net and maybe doing something wrongs.

Thanks

<body>

<!-- Start of Script-->

<script language="VB" runat="server">

        Sub Button1_Click(sender As Object, e As EventArgs)
        Response.Redirect("AdminPage.aspx")
        Session("LoggedIn") = Nothing
        FormsAuthentication.SignOut()
        Label2.Visible = False
        End Sub
    
    </script>
    
<!-- End of Script-->

<!-- start header -->
<div id="header">
	<div id="menu">
		<ul>
			<li class="current_page_item"><a href="default.aspx">Homepage</a>&nbsp; </li>
			<li><a href="blogs.htm">Blogs</a></li>
			<li><a href="photos.htm">Photos</a></li>
			<li><a href="about.htm">About</a></li>  
		</ul>
	</div>
	<!-- ASP BUTTON-->
<h5 align="right" style="width: 908px" ><a href="Default.aspx"><u><asp:Button ID="Label2" 
onclick="Button1_Click" runat="server" Text="LogOut"></asp:Button></u></a></h5>
</div>

IF YOU NEED TO SEE MORE CODE LET ME KNOW.

Hi there, I am not sure why you have this line in the script:

FormsAuthentication.SignOut()

This will force a user to log out and clear the form authentication ticket. Therefore, it will redirect the user to the login page (Default.aspx normally). Try removing that line and let me know how it goes. Hope that fixes it for you!

I removed the line but I got the same result. I will explain what im trying to do. I was just trying to create a simple admin login for my application. when the user logs in I set Session(LoggedIn) = 1 but I can't set the Session to Nothing when the user logs out because the script will not run, so when I run the debugger again the user is already logged in. Maybe there is a better way to do this but I'm learning.

I understand, I think there is a better way of doing that than using scripts. Can you post the code where the administrator is differentiated from normal users?

There is only an admin(It's a personal Web App). Once I've gotten the admin login to work I was going to develop a admin UI page and they would be able to update certain dynamic info, there blog(That I am learning how build : ) ), pictures and some other information.

Are you using MSSQL database as backend store for credentials ?

No since the admin is going to be static I stored the user name and password in my web.config file and used FormsAuthentication to test the user name and password for validity.

Stored username and password

<authentication mode="Forms">
      <forms loginUrl="Login.aspx" protection="All" timeout="10" slidingExpiration="false">
        <credentials passwordFormat="Clear">
          <user name="kiona" password="brice" />
        </credentials>
      </forms>
    </authentication>

Check username and password

Protected Sub LoginUser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginUser.Click

        If FormsAuthentication.Authenticate(UserName.Text, Password.Text) Then
            FormsAuthentication.RedirectFromLoginPage(UserName.Text, False)
            Session("LoggedIn") = 1
        Else
            InvalidLogin.Visible = True
        End If

    End Sub

Login Page Load

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Session("LoggedIn") Is Nothing Then
            Label2.Visible = True
            Label3.Text = Session("LoggedIn").ToString
        Else
            Label2.Visible = False
        End If

    End Sub

What happens if you change :

Session("LoggedIn") = Nothing

TO

Session("LoggedIn") = 1

In your VB script.

The way my code is setup if I change Session to 1 then the session would not end unless I delete the cookie(correct me if I am wrong) using FormsAuthendicated.SignOut() which is what I want to do but it seems like the code is not reaching that line. Maybe I need to try a different appoarch?

Hey majestic0110 thanks for all your help. I really want to get this solved but I have to go but I will be back in a couple of hours. I really appreciate all of your help and and help to come. When I get back I will send you a message and we can continue if thats ok.

Hey thanks for all your help I re-designed my application to use visual studios built in login tools for now. Thanks for all your help.

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.