PDA

View Full Version : ASP.Net Membership System - Login



Cr@iG
Mar 04, 2010, 6:56 AM
Hey, After a little work I've added the asp.net membership functionality to a login window. Any help or improvements welcome

VB.Net:




<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="DriveBusiness_Tracking._Default" %>

<%@ Register assembly="Coolite.Ext.Web" namespace="Coolite.Ext.Web" tagprefix="ext" %>
<%@ Register assembly="Devart.Data.Mysql" namespace="Devart.Data.Mysql" tagprefix="ddm" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As Coolite.Ext.Web.AjaxEventArgs)

'We need to determine if the user is authenticated and set e.Authenticated accordingly
'Get the values entered by the user
Dim loginUsername As String = Username.Text
Dim loginPassword As String = Password.Text

'determine if the user's username/password are valid
If Membership.ValidateUser(loginUsername, loginPassword) Then
FormsAuthentication.SetAuthCookie(loginUsername, True)
Response.Redirect("/desktop.aspx")
Else
e.Success = False
e.ErrorMessage = "Invalid Username or Password"
End If

End Sub


</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="loginhead" runat="server">
<title>Login</title>

</head>
<body>
<form id="form1" runat="server">
<asp:Login ID="Login1" runat="server" Width="312px">
<LayoutTemplate>
<ext:ScriptManager ID="ScriptManager1" runat="server" />
<ext:Window ID="LoginWindow" runat="server" Closable="false" Resizable="false" Height="150" Icon="Lock" Title="Login" Draggable="false" Width="350" Modal="true" BodyStyle="padding:5px;">
<Body>
<ext:FormLayout ID="FormLayout1" runat="server">
<ext:Anchor Horizontal="100%">
<ext:TextField ID="Username" runat="server" ReadOnly="false" FieldLabel="Username" AllowBlank="false" BlankText="Your username is required." />
</ext:Anchor>
<ext:Anchor Horizontal="100%">
<ext:TextField ID="Password" runat="server" ReadOnly="false" InputType="Password" FieldLabel="Password" AllowBlank="false" BlankText="Your password is required." />
</ext:Anchor>
<ext:Anchor Horizontal="100%">
<ext:Checkbox id="RememberMe" runat="server" ReadOnly="false" FieldLabel="Remember Me" />
</ext:Anchor>
</ext:FormLayout>
</body>
<Buttons>
<ext:Button ID="LoginButton" ValidationGroup="Login1" runat="server" CommandName="Login1.Authenticate" Text="Login" Icon="Accept">
<AjaxEvents>
<Click OnEvent="Login1_Authenticate"></Click>
</AjaxEvents>
</ext:Button>
</Buttons>
</ext:Window>
</LayoutTemplate>
</asp:Login>
</form>
</body>

</html>





C#




<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="DriveBusiness_Tracking._Default" %>

<%@ Register assembly="Coolite.Ext.Web" namespace="Coolite.Ext.Web" tagprefix="ext" %>
<%@ Register assembly="Devart.Data.Mysql" namespace="Devart.Data.Mysql" tagprefix="ddm" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Login1_Authenticate(object sender, Coolite.Ext.Web.AjaxEventArgs e)
{

//We need to determine if the user is authenticated and set e.Authenticated accordingly
//Get the values entered by the user
string loginUsername = Username.Text;
string loginPassword = Password.Text;

//determine if the user's username/password are valid
if (Membership.ValidateUser(loginUsername, loginPassword)) {
FormsAuthentication.SetAuthCookie(loginUsername, true);
Response.Redirect("/desktop.aspx");
}
else {
e.Success = false;
e.ErrorMessage = "Invalid Username or Password";

}
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="loginhead" runat="server">
<title>Login</title>

</head>
<body>
<form id="form1" runat="server">
<asp:Login ID="Login1" runat="server" Width="312px">
<LayoutTemplate>
<ext:ScriptManager ID="ScriptManager1" runat="server" />
<ext:Window ID="LoginWindow" runat="server" Closable="false" Resizable="false" Height="150" Icon="Lock" Title="Login" Draggable="false" Width="350" Modal="true" BodyStyle="padding:5px;">
<Body>
<ext:FormLayout ID="FormLayout1" runat="server">
<ext:Anchor Horizontal="100%">
<ext:TextField ID="Username" runat="server" ReadOnly="false" FieldLabel="Username" AllowBlank="false" BlankText="Your username is required." />
</ext:Anchor>
<ext:Anchor Horizontal="100%">
<ext:TextField ID="Password" runat="server" ReadOnly="false" InputType="Password" FieldLabel="Password" AllowBlank="false" BlankText="Your password is required." />
</ext:Anchor>
<ext:Anchor Horizontal="100%">
<ext:Checkbox id="RememberMe" runat="server" ReadOnly="false" FieldLabel="Remember Me" />
</ext:Anchor>
</ext:FormLayout>
</body>
<Buttons>
<ext:Button ID="LoginButton" ValidationGroup="Login1" runat="server" CommandName="Login1.Authenticate" Text="Login" Icon="Accept">
<AjaxEvents>
<Click OnEvent="Login1_Authenticate"></Click>
</AjaxEvents>
</ext:Button>
</Buttons>
</ext:Window>
</LayoutTemplate>
</asp:Login>
</form>
</body>

</html>



Hope this helps

geoffrey.mcgill
Mar 04, 2010, 12:28 PM
Thanks for posting this example!

Cr@iG
Mar 05, 2010, 5:09 AM
No problem geoffrey,

If i get some time over the weekend I'm looking at possibly creating a new <ext:loginwindow> control

Just need to have a proper look through the coolite source

jeeshenlee
May 14, 2010, 9:37 AM
Hello Craig,

Good work! I'm looking forward to use your Login Control :)

FYI, I'm working on a web application that need "Forgot Password", "Add New User" and "Change Password" features. Do you have any sample on these?

Thanks.

Regards,
JeeShen Lee
+012- 904 2869

Zoso
Apr 27, 2011, 2:34 PM
I am trying to use this to replace my normal asp.net logon page but am getting a few errors. It does not recognize the scriptmanager, or AjaxEvents and the username and password fields do not get picked up as being declared.

I am using VS2010, Asp.Net 4.0 and VB for my site design. I am also doing Forms Auth and would really love to replace my current ugly log on window with a nice pretty looking ext window.

Thanks

Vladimir
Apr 27, 2011, 2:42 PM
Hi,

Forms auth forbides access to ext.net http handler. Please see the following topic to solve it

http://forums.ext.net/showthread.php?13385-bypassing-authentication-for-EXTJS-EXTNET-resources

bmagana
Jun 29, 2011, 4:02 PM
Hi Vlad,

Im trying to implement this sample but the username and password fields are not recognized. I read the link above you post, with no luck...

The web.config already have:



<configuration>
<configSections>
<section name="extnet" type="Ext.Net.GlobalConfig" requirePermission="false" />
</configSections>

<system.web>
<httpHandlers>
<add path="*/ext.axd" verb="*" type="Ext.Net.ResourceHandler" validate="false"/>
</httpHandlers>
</system.web>
....
</configuration>


also I added the permissions in the web.config:


<location path="extjs">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="extnet">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="icons">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="ux">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>


Im using ext.net v1, VS2010 and .net 4.

Any suggestions? Thanks!