Roles And Membership
by
Imran
02/June/2008
|
 |
Web.Config
<connectionStrings>
<add name="Test" connectionString="Data
Source=imran\SqlExpress;Initial Catalog=Test;Integrated Security=True"
providerName="System.Data.SqlClient"/> </connectionStrings>
<system.web> <!--By imran For Membership And Roles--> <authentication mode="Forms"> <forms name=".ASPXAUTH" loginUrl="Login.aspx" protection="All" timeout="30" path="/" requireSSL="false" slidingExpiration="true" defaultUrl="Login.aspx" cookieless="UseCookies" enableCrossAppRedirects="false"/> </authentication> <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20"> <providers> <clear/> <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="Test" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" passwordFormat="Hashed" applicationName="/"/> </providers> </membership> <roleManager enabled="true" cacheRolesInCookie="true" defaultProvider="SqlProvider" cookieName=".ASPXROLES" cookiePath="/" cookieTimeout="30" cookieRequireSSL="false" cookieSlidingExpiration="true" createPersistentCookie="false" cookieProtection="All"> <providers> <add name="SqlProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="Test" applicationName="SecurityQuickStart"/> </providers> </roleManager>
Login.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="_Login" MasterPageFile="~/MasterPage.master" %> <asp:Content ContentPlaceHolderID=CPHBody runat=server> <table class="style1"> <tr> <td align="right"> UserName:</td> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right"> Password:</td> <td> <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox> </td> </tr> <tr> <td align="right"> </td> <td> <asp:Label ID="lblErr" runat="server" ForeColor="#FF6600"></asp:Label> </td> </tr> <tr> <td align="right"> </td> <td> <asp:Button ID="Button1" runat="server" Text="Submit" /> </td> </tr> </table> </asp:Content>
Login.aspx.vb
Partial Class _Login Inherits System.Web.UI.Page
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete If Not Page.IsPostBack Then 'Role User 'Coach is Coach 'Admin is Imran 'Roles.CreateRole("Coach") 'Membership.CreateUser("Asif", "123456@", "Asif@msn.com") 'Roles.AddUserToRole("Asif", "Coach") 'Membership.CreateUser("Imran", "123456@", "Imran@msn.com") 'Roles.AddUserToRole("Imran", "Admin") End If End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click ' Is the user valid? If (Membership.ValidateUser(TextBox1.Text, TextBox2.Text)) Then FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, False) 'Response.Redirect("Secure\Secure.aspx") Else lblErr.Text = "Invalid Credentials: Please try again" End If End Sub End Class
http://msdn.microsoft.com/en-us/library/ms998310.aspx http://quickstarts.asp.net/QuickStartv20/aspnet/doc/security/membership.aspx http://www.informit.com/articles/article.aspx?p=351414
|
|
|