Display Login box at a normal page

Topics: Developer Forum, User Forum
Jan 6, 2011 at 10:29 AM
Edited Jan 6, 2011 at 10:30 AM

Hi

I need to display the login box on a normal page e.g. offers.aspx... and then the power users logs in by that  

how can this be done ?

Jan 28, 2011 at 5:37 PM

Hi Wiper,

The easiest solution is to create an EasyControl under your EasyControls folder. Then drop an EasyControl on to your page and set it to load the Login control.

My sample login code:

EasyControls/Login.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Login.ascx.cs" Inherits="SectionControls_Login" %>

<%--Login--%>
<asp:Login ID="Login1" runat="server" Width="100%" FailureText="<%$ Resources:stringsRes, pge_Login_Failure%>" OnLoggingIn="OnLoggingIn" OnLoggedIn="OnLoggedIn">
	<LayoutTemplate>
		<div class="ConektLogin_Default">
			<%--Not Authenticated--%>
			<asp:Panel ID="pnlNotLoggedIn" runat="server" DefaultButton="LoginButton">
				<h1><conekt:WebSiteTitle runat="server" /> Account Login</h1>
				<h4>Please enter your login information below.</h4>
				<asp:Table ID="tblLogin" runat="server" Width="" CellPadding="0" CellSpacing="0">
					<%--Failure--%>
					<asp:TableRow>
						<asp:TableCell ColumnSpan="2">
							<asp:Localize ID="FailureText" runat="server" EnableViewState="False" />
						</asp:TableCell>
					</asp:TableRow>
							
					<%--Username--%>
					<asp:TableRow>
						<asp:TableCell CssClass="clLabel">
							<asp:Label ID="UsernameLabel" runat="server" AssociatedControlID="UserName" Text="<%$ Resources:stringsRes, pge_Login_UserName%>" />:
						</asp:TableCell>
						<asp:TableCell CssClass="clUsernameTextbox">
							<div class="Relative">
								<asp:TextBox ID="UserName" runat="server" />
								<asp:RequiredFieldValidator ID="rfvUsername" runat="server" CssClass="StatusWarning" ControlToValidate="UserName" ErrorMessage="<%$ Resources:stringsRes, pge_Login_UserNameRequired%>" ToolTip="<%$ Resources:stringsRes, pge_Login_UserNameRequired%>" ValidationGroup='<%#Login1.UniqueID%>' />
							</div>
						</asp:TableCell>
					</asp:TableRow>
							
					<%--Password--%>
					<asp:TableRow>
						<asp:TableCell CssClass="clLabel">
							<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" Text="<%$ Resources:stringsRes, pge_Login_Password%>" />:
						</asp:TableCell>
						<asp:TableCell CssClass="clPasswordTextbox">
							<div class="Relative">
								<asp:TextBox ID="Password" runat="server" TextMode="Password" />
								<asp:RequiredFieldValidator ID="rfdRequired" runat="server" CssClass="StatusWarning" ControlToValidate="Password" ErrorMessage="<%$ Resources:stringsRes, pge_Login_PasswordRequired%>" ToolTip="<%$ Resources:stringsRes, pge_Login_PasswordRequired%>" ValidationGroup='<%#Login1.UniqueID%>' />
							</div>
						</asp:TableCell>
					</asp:TableRow>

					<%--Commands--%>
					<asp:TableRow>
						<asp:TableCell></asp:TableCell>
						<asp:TableCell CssClass="clCommands">
							<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="<%$ Resources:stringsRes, pge_Login_LogIn%>" ValidationGroup="<%#Login1.UniqueID%>" />
						</asp:TableCell>
					</asp:TableRow>
				</asp:Table>
						
				<asp:HyperLink runat="server" ID="lnkChangePassword" Text="<%$ Resources:stringsRes, pge_Login_ChangePassword%>" NavigateUrl="~/ChangePassword.aspx" Visible="false" />
				<asp:Placeholder ID="trActivationSuccess" runat="server" Visible="false">
					<asp:Localize runat="server" Text="<%$ Resources:stringsRes, pge_Login_ActivationSuccess%>" />
				</asp:Placeholder>
				<asp:Placeholder ID="trCreateUser" runat="server" Visible="false">
					<asp:HyperLink runat="server" ID="lnkCreateUser" Text="<%$ Resources:stringsRes, pge_Login_CreateUser%>" NavigateUrl="~/UserRegistration.aspx" />
				</asp:Placeholder>
			</asp:Panel>
			<%--End Not Authenticated--%>
					
			<%--Is Authenticated--%>
			<asp:Panel ID="pnlLoggedIn" runat="server" Width="100%" CssClass="glLoggedIn">
				<h2>You are already logged in to your <conekt:WebSiteTitle ID="WebSiteTitle2" runat="server" /> account</h2>
				<asp:HyperLink ID="hplHomePage" runat="server" Text="Return to the HomePage" NavigateUrl="~/" />
			</asp:Panel>
			<%--End Is Authenticated--%>
		</div>
	</LayoutTemplate>
</asp:Login>
<%--End Login--%>

EasyControls/Login.ascx.cs

//===============================================================================================
//
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
//
//===============================================================================================

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebPagesStarterKit;
using MyWebPagesStarterKit.Controls;
using System.Web.Security;

public partial class SectionControls_Login : System.Web.UI.UserControl
{
	private string _returnURL = string.Empty;

	protected void Page_Init(object sender, EventArgs e)
	{
		if (!IsPostBack && !string.IsNullOrEmpty(Request.QueryString["activate"]))
		{
			Guid userId = new Guid(Request.QueryString["activate"]);
			MembershipUser user = Membership.GetUser(userId);
			user.IsApproved = true;
			Membership.UpdateUser(user);

			Login1.UserName = user.UserName;

			Login1.FindControl("trActivationSuccess").Visible = true;
		}

		//Login1.FindControl("trCreateUser").Visible = _website.AllowUserSelfRegistration;
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		Panel pnlNotLoggedIn = (Panel)Login1.FindControl("pnlNotLoggedIn");
		Panel pnlLoggedIn = (Panel)Login1.FindControl("pnlLoggedIn");
		TextBox UserName = (TextBox)Login1.FindControl("UserName");

		if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
		{
			pnlLoggedIn.Visible = true;
			pnlNotLoggedIn.Visible = false;
		}
		else
		{
			pnlLoggedIn.Visible = false;
			pnlNotLoggedIn.Visible = true;
		}

		if (!IsPostBack)
		{
			Control ctl = Login1.FindControl("UserName");
			if (ctl != null)
			{
				(ctl).Focus();
			}

			//hack for maintaining the virtual url
			if (Session["OriginalURL"] != null)
				ViewState["ReturnURL"] = Session["OriginalURL"].ToString();
		}
		else
		{
			//hack for maintaining the virtual url
			if (ViewState["ReturnURL"] != null)
				_returnURL = ViewState["ReturnURL"].ToString();
		}
	}

	protected void OnLoggingIn(object sender, EventArgs e)
	{
		//hack for upper and lower case: the asp login control is case sensitive: Admin <> admin
		//in order to make it not case sensitive, we rewrite the UserName (as it should be) to the login control
		TextBox txtUserName = (TextBox)Login1.FindControl("UserName");
		foreach (MembershipUser user in Membership.GetAllUsers())
		{
			if (string.Compare(user.UserName, txtUserName.Text, true) == 0)
			{
				Login1.UserName = user.UserName;
				return;
			}
		}

		//Conekt#026 Admin Controls Cookie
		if (Request.Cookies["showAdminTools"] == null)
		{
			HttpCookie _showAdminTools = new HttpCookie("showAdminTools");
			_showAdminTools.Value = "True";
			_showAdminTools.Expires = DateTime.Now.AddDays(30);
			Response.Cookies.Add(_showAdminTools);
		}
	}

	/// <summary>
	/// hack for maintaining the virtual url
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void OnLoggedIn(object sender, EventArgs e)
	{
		if (_returnURL != string.Empty)
			Response.Redirect(_returnURL);
	}
	
}

Jan 31, 2011 at 10:31 AM

thanks - It's working perfect... :0)

Jan 31, 2011 at 2:24 PM

Glad to hear it!

Bjorn

Feb 1, 2011 at 10:29 AM

Hi Bjorn

ohh. I discovered that after login I get redirected to the homepage...

Is it possible to stay on the page where I have the easycontrol / login box?

 

Feb 1, 2011 at 2:31 PM

Hi Wiper,

To redirect to a custom page, edit Login.ascx.cs as follows;

protected void OnLoggedIn(object sender, EventArgs e)
{
	if (_returnURL != string.Empty)
		Response.Redirect(_returnURL);
	else //Custom redirect
		Response.Redirect("/SomePage.aspx")
}

Bjorn