unexpected authectication behavior in ASP.NET example app. Apress

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: ramonred (ramonred_at_discussions.microsoft.com)
Date: 01/02/05


Date: Sun, 2 Jan 2005 12:23:03 -0800

Hi,
I am following along an example from the book <b>Beginning Visual Web
Programming in C#</b>

My environment is this:
IIS 5.1 on XP Pro, I have the checkbox 'use windows integrated
authentication' unchecked, and I am allowing anonymous access.

Prior to building the example that is giving the error, the app would ask
you to login if you weren't already logged in by redirecting you to the login
page.

In the case of the example, for testing purposes you are instructed to enter
 a url with a parameter appened to the URL. What is supposed to happen is
you will be redirected to the login page, then once you login you should be
redirected back to the original page that was in the url with the appended
parameter, that parameter being used to pull some data from the db and
display on this page.

I have carefully checked my code, and even used the authors code downloaded
from apress with the same error. I will post the code, but what I am really
looking for is a general guideline how to debug this issue. I am wondering if
this is a bug, or error in their program.

I am new to C# and asp.net, looking at this as a learning opportunity, but
also feeling frustrated.

viewuser.aspx
<code>
<%@ Page language="c#" Codebehind="viewuser.aspx.cs" AutoEventWireup="false"
Inherits="FriendsReunion.viewuser" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
        <HEAD>
                <title>viewuser</title>
                <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
                <meta name="CODE_LANGUAGE" Content="C#">
                <meta name="vs_defaultClientScript" content="JavaScript">
                <meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
                <LINK href="styles/iestyle.css" type="text/css" rel="stylesheet">
        </HEAD>
        <body MS_POSITIONING="FlowLayout">
                <form id="Form1" method="post" runat="server">
                        <TABLE class="TableLines" id="tbLogin" cellSpacing="2" cols="2"
cellPadding="2" border="0">
                                <TR>
                                        <TD align="right" colSpan="1" rowSpan="1">
                                                <P align="right">name:</P>
                                        </TD>
                                        <TD>
                                                <asp:Label id="lblName" runat="server"
                                                Text='<%# dsUser.Tables[0].Rows[0]["FirstName"] + " " +
dsUser.Tables[0].Rows[0]["LastName"] %>'>
                                                </asp:Label>
                                        </TD>
                                </TR>
                                <TR>
                                        <TD>birth date:</TD>
                                        <TD>
                                                <asp:Label id="lblBirth" runat="server"
                                                Text='<%#
DataBinder.Eval(dsUser.Tables[0].Rows[0],"[DateOfBirth]","{0:MMMM dd,
yyyy}")%>'>
                                                </asp:Label>
                                        </TD>
                                </TR>
                                <TR>
                                        <TD>
                                                <P align="right">phone number:</P>
                                        </TD>
                                        <TD>
                                                <asp:Label id="lblPhone" runat="server"
                                                Text='<%#
DataBinder.Eval(dsUser.Tables["User"].Rows[0],"[PhoneNumber]") %>' >
                                                </asp:Label>
                                        </TD>
                                </TR>
                                <TR>
                                        <TD>
                                                <P align="right">mobile number:</P>
                                        </TD>
                                        <TD>
                                                <asp:Label id="lblMobile" runat="server"
                                                Text='<%# dsUser.Tables["User"].Rows[0]["CellNumber"] %> '>
                                                </asp:Label>
                                        </TD>
                                </TR>
                                <TR>
                                        <TD>
                                                <P align="right">address:</P>
                                        </TD>
                                        <TD>
                                                <asp:Label id="lblAddress" runat="server"
                                            Text='<%#
DataBinder.Eval(dsUser.Tables["User"].Rows[0],"[Address]") %> '>
                                                </asp:Label>
                                        </TD>
                                </TR>
                                <TR>
                                        <TD>
                                                <P align="right">email:</P>
                                        </TD>
                                        <TD>
                                                <asp:HyperLink id="lnkEmail" runat="server"
                                                NavigateUrl='<%#
DataBinder.Eval(dsUser.Tables["User"].Rows[0],"[Email]","mailto:{0}") %> '>
                                                send mail</asp:HyperLink>
                                        </TD>
                                </TR>
                        </TABLE>
                        <P>the user
                                <asp:Label id="lblPending" runat="server"
                                Text='<%# GetPending() %>'>
                                </asp:Label> requests for contact is pending.</P>
                        <asp:Button id="btnAuthorize" runat="server" Text="authorize
contact"></asp:Button>
                </form>
        </body>
</HTML>
</code>
viewuser.aspx.cs
<code>

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace FriendsReunion
{
        /// <summary>
        /// Summary description for viewuser.
        /// </summary>
        public class viewuser : FriendsBase
        {
                protected System.Web.UI.WebControls.Label lblName;
                protected System.Web.UI.WebControls.Label lblBirth;
                protected System.Web.UI.WebControls.Label lblPhone;
                protected System.Web.UI.WebControls.Label lblMobile;
                protected System.Web.UI.WebControls.Label lblAddress;
                protected System.Web.UI.WebControls.Label lblPending;
                protected System.Web.UI.WebControls.HyperLink lnkEmail;
                protected System.Web.UI.WebControls.Button btnAuthorize;
                protected DataSet dsUser;
        
                private void Page_Load(object sender, System.EventArgs e)
                {
                        string userID = Request.QueryString["RequestID"];
                        
                        //ensure we receive an ID
                        if (userID == null)
                                        throw new ArgumentException("this page expects a RequestID parameter");

                        //create the connection and data adaptor
                        SqlConnection cnFriends = new SqlConnection(
                                ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
                        SqlDataAdapter adUser = new SqlDataAdapter(
                                "SELECT * FROM [User] WHERE UserID=@ID", cnFriends);
                        adUser.SelectCommand.Parameters.Add("@ID", userID);

                        //initialize the dataset and fill it with data
                        dsUser = new DataSet();
                        adUser.Fill(dsUser, "User");

                        //finally, bind all controls on the page 'one control to rule.... and in
the darkness
                        //bind them'. no wait wrong venue!
                        this.DataBind();
                }

                protected string GetPending()
                {
                        //create the connection and command to execute
                        SqlConnection cnFriends = new SqlConnection(
                                ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
                        SqlCommand cmd = new SqlCommand(
                                @"SELECT COUNT(*) FROM Contact
                  WHERE IsApproved=0 AND DestinationID=@ID",
                                cnFriends);
                        cmd.Parameters.Add("@ID", Page.User.Identity.Name);
                        cnFriends.Open();

                        try
                        {
                                return cmd.ExecuteScalar().ToString();
                        }
                        finally
                        {
                                cnFriends.Close();
                        }
                }

                #region Web Form Designer generated code
                override protected void OnInit(EventArgs e)
                {
                        //
                        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                        //
                        InitializeComponent();
                        base.OnInit(e);
                }
                
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
                        this.btnAuthorize.Click += new
System.EventHandler(this.btnAuthorize_Click);
                        this.Load += new System.EventHandler(this.Page_Load);

                }
                #endregion

                private void btnAuthorize_Click(object sender, System.EventArgs e)
                {
                        //create the connection and command to execute
                        SqlConnection cnFriends = new SqlConnection(
                                ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
                        SqlCommand cmd = new SqlCommand(
                                @"UPDATE Contact
                                        SET IsApproved=1
                    WHERE
                                                RequestID=@RequestID AND DestinationID=@DestinationID",
                                cnFriends);
                        cmd.Parameters.Add("@RequestID", Request.QueryString["RequestID"]);
                        cmd.Parameters.Add("@DestinationID", Page.User.Identity.Name);

                        cnFriends.Open();

                        try
                        {
                                cmd.ExecuteNonQuery();
                        }
                        finally
                        {
                                cnFriends.Close();
                        }
                        //return to the news page
                        Response.Redirect("news.aspx");

                }
        }
}
</code>



Relevant Pages

  • Re: missing session variables problem
    ... The index.php page accepts login and password info, ... clearance level ... are stored in session variables and the user is redirected to ... you're redirecting to http://www.example.com? ...
    (comp.lang.php)
  • Re: Authentication login screen appears on both frames of the framset
    ... wouldn't be redirecting to the login. ... give all users permission to the page that shouldn't redirect, ... >> If you're using forms authentication when a user is not authorized to see ... >> page they are automatically redirected to the login page. ...
    (microsoft.public.dotnet.framework.aspnet)
  • RedirectFromLoginPage problem, not redirecting to members page
    ... I am having problem with redirecting the user form the login page to members ... page using RedirectFromLoginPage method. ... add new folder and name it as 'members'. ...
    (microsoft.public.dotnet.security)
  • Re: Login security for a form
    ... If its redirecting to the login page it sounds like either the cookie ... login.aspx to authenticate me. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: A newbie question
    ... Pretty much all the things you ask for are provided by Asp.Net with virtually no programming necessary (including the Login dialog). ... When a user enters a User ID and a password, the program checks whether the user has access rights against some database, Excel file or some data storage and if so opens a web page. ... I also need to be able to have means to enter the UserId and Passwords of the clients to this database. ...
    (microsoft.public.dotnet.general)