Search This Blog

Wednesday, October 24, 2018

ASP.Net Membership and Role Provider

Step:1
Welcome to designer blog, Here we are going to discuss about  how to provide ASP.Net Membership and Role Provider.

Step:2

Install the aspnet_regsql.exe by going to this location path :

(C:\Windows\Microsoft.NET\Framework[framework version]\aspnet_regsql) 

Example:

(C:\Windows\Microsoft.NET\Framework\v4.0.30319)

Step:3

After installing the Aspnet_regsql.exe. If you open the SQL Server, you can find the ASP.Net Membership Tables is installed in the SQL server.

Step:4

Database provides the API calls to invoke installed  tables in SQL server. Open the web.config file and declare the connection String, Membership and Role Manager.

Below are the declaration code:

  <connectionStrings>
    <add name="LocalSqlServer3" connectionString="Data Source=localhost;Initial Catalog=credentials;Integrated Security=True;Pooling=False"/>

  </connectionStrings>

 <membership>
        <providers>
          <remove name="AspNetSqlMembershipProvider"/>
          <add name="AspNetSqlMembershipProvider"
          type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
          connectionStringName="LocalSqlServer3"
          enablePasswordRetrieval="false"
          enablePasswordReset ="true"
          applicationName="/"         
          minRequiredPasswordLength="7"/>
        </providers>
      </membership>

      <roleManager enabled="true" >
        <providers>
          <clear/>
          <add name="AspNEtSqlRoleProvider"
              connectionStringName="LocalSqlServer3"
               applicationName="/"
              type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
      </roleManager>

Step5:

Create a login screen and drag and drop the login and register wizard UI Controls

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="CustomerdataEntryWeb.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-family:Arial">
<table style="border: 1px solid black">
    <tr>
        <td colspan="2">
            <b>Login</b>
        </td>
    </tr>
    <tr>
        <td>
            User Name
        </td>    
        <td>
            :<asp:TextBox ID="txtUserName" runat="server">
            </asp:TextBox>
        </td>    
    </tr>
    <tr>
        <td>
            Password
        </td>    
        <td>
            :<asp:TextBox ID="txtPassword" TextMode="Password" runat="server">
            </asp:TextBox>
        </td>    
    </tr>
    <tr>
        <td>
                    
        </td>    
        <td>
            <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
        </td>    
    </tr>
</table>
<br />
<a href="Registration/Register.aspx">Click here to register</a> 
if you do not have a user name and password.
</div>
        <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
        </asp:Login>
        <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" OnCreatedUser="CreateUserWizard1_CreatedUser">
            <WizardSteps>
                <asp:CreateUserWizardStep runat="server" />
                <asp:CompleteWizardStep runat="server" />
            </WizardSteps>
        </asp:CreateUserWizard>
    </form>
</body>
</html>

Step6:

Include the namespace "using System.Web.Security" and write the below code in the code behind for Login and Register buttons

Code Behind:

   protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            if(Membership.ValidateUser(Login1.UserName, Login1.Password))
            {
                Response.Redirect("welcome.aspx");
            }
        }

        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            MembershipCreateStatus status;
            Membership.CreateUser(CreateUserWizard1.UserName,
                                     CreateUserWizard1.Password,
                                    CreateUserWizard1.Email,
                                    CreateUserWizard1.Question,
                                    CreateUserWizard1.Answer,
                                    true,
                                    out status);

            if(status != MembershipCreateStatus.Success)
            {
                status.ToString();
            }
            else
            {
                Response.Write("Created");
            }
        }

Step7:

Thanks for reading article.Stay Tune on www.webdesignersdairy.com  for more updates.


Enjoy Folks

No comments:

Post a Comment

Validating to select in sequencial order using angular

    < input type = "checkbox" (change) = "handleSelectTaskItem($event, taskItem)" [checked] = " taskItem . i...