web page requests not redirected to Login Page

web page requests not redirected to Login Page

Page 1 of 1

4 Replies - 11087 Views - Last Post: 24 February 2010 - 04:35 AM Rate Topic: -----

#1 Sujit P  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 23-February 10

web page requests not redirected to Login Page

Posted 23 February 2010 - 07:44 AM

Hi All,

I have created a web application in framework 3.5. The Default.aspx page redirects to Login Page. However, when you request any other page from the application the user is not re-directed to the login page if he is not logged in. What is the best way to ensure that a user is first logged in before he requests any page?

Please post your suggestions.

Thanks in advance.
Sujit P
Is This A Good Question/Topic? 0
  • +

Replies To: web page requests not redirected to Login Page

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: web page requests not redirected to Login Page

Posted 23 February 2010 - 08:51 AM

Are you using Membership in your web site or a simple login system?
Was This Post Helpful? 0
  • +
  • -

#3 Sujit P  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 23-February 10

Re: web page requests not redirected to Login Page

Posted 23 February 2010 - 09:10 PM

Hi Jayman,

Thanks for replying immediately.

Yes, I am using Membership in my application to authenticate users and redirect them based on their roles.

When you run this application by typing the address of any page in the URL, the requested page is displayed without even asking the user to Login. If the user is not logged in.

I am pasting the code for the Default.aspx, Default.cs, Login.aspx and Login.cs.

1>Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Golden_Homes._Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>



2>Default.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Golden_Homes
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }
    }
}



3>Login.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Login.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<!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>Untitled Page</title>
    <style type="text/css">
        body
        {
            margin-left: 0px;
            margin-top: 0px;
            margin-right: 0px;
            margin-bottom: 0px;
            
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="d1" runat="server">
        <asp:Image id="ghBanner" runat="server" ImageUrl="~/Images/goldentopbnr1.jpg"/>
    </div>
    <div id="d2" runat="server" style="position:absolute; left:250px ;top:200px"> 
    <table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">    
       <tr>
         <td bgcolor="">                                       
                                               
            <asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Temp.aspx">                                           

                <LayoutTemplate>                                                        
                    <table border="0" cellpadding="2" cellspacing="2" width="300px">
                        <tr>
                            <td align="center" colspan="2">
                                Log In<br /><br />
                            </td>
                        </tr>
                        <tr>
                            <td align="left">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User 
                                                                                    Name:</asp:Label>
                             </td>
                            <td>
                                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                         ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                             </td>
                        </tr>
                        <tr>
                             <td align="left">
                                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                             </td>
                             <td>
                                <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2" style="color: Red;">
                                <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                       </tr>
                        <tr>
                            <td align="center" colspan="2">
                                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                                ValidationGroup="Login1" onclick="LoginButton_Click" />
                            </td>
                        </tr>                        
                </table>
            </td>
        </tr>
     </table>
   </LayoutTemplate>
 </asp:Login>
 </td>     
 </tr>                                        
 </table>                  
</div>                 
 </form>
</body>
</html>



4>Login.cs
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void LoginButton_Click(object sender, EventArgs e)
    {
           

    }
}




Thanks in advance.
Sujit P

This post has been edited by eclipsed4utoo: 24 February 2010 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 Sujit P  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 23-February 10

Re: web page requests not redirected to Login Page

Posted 23 February 2010 - 11:11 PM

Hi Jayman,

Thanks for suggestion.
I have found a solution.
I am able to reolve this problem by changing configuration settings in web.config.
I have added the following lines:-

<location path="Admin">
        <system.web>
            <authorization>
                <allow roles="Admin" />
                <deny users="*" />
            </authorization>
        </system.web>
    </location>
    <location path="Customers">
        <system.web>
            <authorization>
                <allow roles="Customer" />
                <deny users="*" />
            </authorization>
        </system.web>
    </location>
    <location path="Promoters">
        <system.web>
            <authorization>
                <allow roles="Promoter" />
                <deny users="*" />
            </authorization>
        </system.web>
    </location>

 <roleManager enabled="true" cacheRolesInCookie="true"/>



Regds,
Sujit P

This post has been edited by eclipsed4utoo: 24 February 2010 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 Sujit P  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 23-February 10

Re: web page requests not redirected to Login Page

Posted 24 February 2010 - 04:35 AM

Hi all,

You can further add the following lines to Page_Load event of all the pages for Secure Login.

if(!Context.User.Identity.IsAuthenticated)
                 Response.Redirect("~/Login.aspx");


Regds,
Sujit P

This post has been edited by eclipsed4utoo: 24 February 2010 - 07:22 AM
Reason for edit:: please use the code tags.. [code] your code here [/code]

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1