About the author

Imran Imran
Like Development in DotNet.

E-mail me Send mail

Recent Articles

 
1: Roles And Membership
Category: ASP.Net
Added: 6/2/2008

 
2: ListView and DataPager in ASP.NET 3.5
Category: ASP.Net
Added: 5/15/2008

 
3: Asp.Net Cookies
Category: ASP.Net
Added: 5/8/2008

 
4: DatagridDatalistRepeater
Category: ASP.Net
Added: 5/8/2008

 
5: mySql With Asp.Net
Category: Databases
Added: 4/16/2008

 
6: MDF without LDF
Category: Databases
Added: 4/16/2008

 
7: Disable Right Click
Category: Java Script
Added: 4/16/2008

 
8: String Functions
Category: ASP.Net
Added: 3/20/2008

 
9: Water Mark
Category: HTML & DHTML
Added: 3/20/2008

 
10: Ajax With Javascript
Category: AJAX
Added: 3/10/2008

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">
                &nbsp;</td>
            <td>
                <asp:Label ID="lblErr" runat="server" ForeColor="#FF6600"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="right">
                &nbsp;</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


Powered by DotNetClassic.com