Create user and Give its Authentication To page access


Creat.aspx


  <fieldset>
        <legend>Create User</legend>
        <center>
            <table cellpadding="10">
                <tr>
                    <td>
                        Enter Username:-
                    </td>
                    <td>
                        <asp:TextBox ID="txtname" runat="server"></asp:TextBox><asp:RequiredFieldValidator
                            ID="a" runat="server" ValidationGroup="valid" ControlToValidate="txtname" ErrorMessage="***"
                            ForeColor="Red" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Enter Password:-
                    </td>
                    <td>
                        <asp:TextBox ID="txtpass" runat="server" TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator
                            ValidationGroup="valid" ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtpass"
                            ErrorMessage="***" ForeColor="Red" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Label ID="lblmsg" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
            <asp:Label ID="lblmenu" runat="server" Text="Home"></asp:Label>
            <asp:CheckBoxList ID="chksub" runat="server" RepeatColumns="3" RepeatLayout="Table">
            </asp:CheckBoxList>
            <asp:Label ID="lblmen" runat="server" Text="GuestBook"></asp:Label>
            <asp:CheckBoxList ID="chk1" runat="server" RepeatColumns="3" RepeatLayout="Table">
            </asp:CheckBoxList>
            <br />
            <asp:Button ID="btnsubmit" runat="server" ValidationGroup="valid" Text="SUBMIT" CssClass="button"
                OnClick="btnsubmit_Click" />
            <br />
            <table>
                <tr>
                    <td>
                        <asp:Button ID="btnupdate" runat="server" Visible="false" ValidationGroup="valid"
                            Text="UPDATE" OnClick="btnupdate_Click" />
                    </td>
                    <td>
                        <asp:Button ID="btncanel" runat="server" Visible="false" Text="CANCEL" OnClick="btncanel_Click" />
                    </td>
                </tr>
            </table>
            <div>
                <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" DataKeyNames="id"
                    OnSelectedIndexChanging="grd_SelectedIndexChanging">
                    <Columns>
                        <asp:CommandField ShowSelectButton="true" HeaderText="Select" />
                        <asp:BoundField DataField="Usename" HeaderText="Name" />
                        <asp:BoundField DataField="password" HeaderText="Password" />
                    </Columns>
                </asp:GridView>
            </div>
        </center>
    </fieldset>





Creat.aspx.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
public partial class creat : System.Web.UI.Page
{
    Dal odal = new Dal();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fil();
            fill();
        }

    }
    public void fill()
    {
        string str = "select * from adminlogin where id > '1'";
        dt = odal.selectbyquerydt(str);
        grd.DataSource = dt;
        grd.DataBind();
    }

    public void fil()
    {
        string str = "select id,submenu from user_sub_menu where menu='Home'";
        dt = odal.selectbyquerydt(str);
        chksub.DataSource = dt;
        chksub.DataTextField = "submenu";
        chksub.DataValueField = "id";
        chksub.DataBind();
        string str1 = "select id,submenu from user_sub_menu where menu='GuestBook'";
        dt = odal.selectbyquerydt(str1);
        chk1.DataSource = dt;
        chk1.DataTextField = "submenu";
        chk1.DataValueField = "id";
        chk1.DataBind();
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        dt = odal.selectbyquerydt("select usename from adminlogin where usename='" + txtname.Text + "' ");
        if (dt.Rows.Count == 0)
        {
            string insert = "insert into adminlogin values('" + txtname.Text + "','" + txtpass.Text + "','1') select scope_identity()";
            dt = odal.selectbyquerydt(insert);
            lblmsg.Text = "The User Is added";
            int id = Convert.ToInt16(dt.Rows[0][0].ToString());


            string sub = "";
            string sub1 = "";
            string total = "";
            if (chksub != null)
            {
                foreach (ListItem s in chksub.Items)
                {
                    if (s.Selected)
                    {
                        sub += s + ",";

                    }
                }
            }
            if (chk1 != null)
            {
                foreach (ListItem s in chk1.Items)
                {
                    if (s.Selected)
                    {
                        sub1 += s + ",";

                    }
                }
            }
            total = sub + sub1;

            odal.updatedata("insert into usermaster (userid,pageid,isactive) values ('" + id + "','" + total + "','1')");
            fill();
            txtname.Text = "";
            for (int j = 0; j < chksub.Items.Count; j++)
            {
                if (chksub.Items[j].Selected)
                {
                    chksub.Items[j].Selected = false;

                }
            }
            for (int i = 0; i < chk1.Items.Count; i++)
            {
                if (chk1.Items[i].Selected)
                {
                    chk1.Items[i].Selected = false;

                }
            }
        }


    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        string ite = "";
        string sub1 = "";
        string total = "";
        if (chksub != null)
        {
            foreach (ListItem s in chksub.Items)
            {
                if (s.Selected)
                {
                    ite += s + ",";


                }
            }
        }
        if (chk1 != null)
        {
            foreach (ListItem s in chk1.Items)
            {
                if (s.Selected)
                {
                    sub1 += s + ",";

                }
            }
        }
        total = ite + sub1;
        odal.updatedata("update usermaster set pageid='" + total + "' where userid='" + Convert.ToInt16(ViewState["id"].ToString()) + "'");
        fill();
        txtname.Text = "";
        for (int j = 0; j < chksub.Items.Count; j++)
        {
            if (chksub.Items[j].Selected)
            {
                chksub.Items[j].Selected = false;
            }
        }
        for (int i = 0; i < chk1.Items.Count; i++)
        {
            if (chk1.Items[i].Selected)
            {
                chk1.Items[i].Selected = false;

            }
        }
        btncanel.Visible = false;
        btnsubmit.Visible = true;
        btnupdate.Visible = false;
    }
    protected void btncanel_Click(object sender, EventArgs e)
    {
        int count = chksub.Items.Count;
        for (int j = 0; j < count; j++)
        {
            if (chksub.Items[j].Selected)
            {
                chksub.Items[j].Selected = false;
            }
        }
        for (int j = 0; j < chk1.Items.Count; j++)
        {
            if (chk1.Items[j].Selected)
            {
                chk1.Items[j].Selected = false;
            }
        }
        btncanel.Visible = false;
        btnupdate.Visible = false;
        btnsubmit.Visible = true;

        txtname.Text = "";
        txtpass.Text = "";
        txtname.ReadOnly = false;
    }
    protected void grd_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        grd.SelectedIndex = e.NewSelectedIndex;
        int id = int.Parse(grd.DataKeys[grd.SelectedIndex].Value.ToString());
        string sele = "select u.pageid,a.usename,u.id  from usermaster as u left join adminlogin as a on a.id=u.userid where userid=" + id;
        dt = odal.selectbyquerydt(sele);
        ViewState["id"] = id;
        string files = dt.Rows[0]["pageid"].ToString();
        txtname.Text = dt.Rows[0]["usename"].ToString();
        btnsubmit.Visible = false;
        btnupdate.Visible = true;
        btncanel.Visible = true;
        if (files.IndexOf(",") == 0)
        {
            files = files.Remove(0, 1);
        }

        if (files != "")
        {
            char[] sep = { ',' };

            string[] filearray = files.Split(sep);


            int count = chksub.Items.Count;
            int count1 = chk1.Items.Count;
            for (int j = 0; j < count; j++)
            {
                if (filearray.Contains(chksub.Items[j].Text))
                {
                    chksub.Items[j].Selected = true;

                }
            }
            for (int i = 0; i < count1; i++)
            {
                if (filearray.Contains(chk1.Items[i].Text))
                {
                    chk1.Items[i].Selected = true;

                }
            }
        }
    }
}




Masterpage.master



<body>
        <form id="form1" runat="server">
        <div>
            <header>
    <div class="main">
     <div id="search1">
       
         <asp:TextBox ID="text" runat ="server"  Text ="Searching ..." onBlur="if(this.value=='') this.value='Searching ...'" onFocus="if(this.value =='Searching ...' ) this.value=''"></asp:TextBox>
            <a onClick="document.getElementById('search1').submit()"></a>
        </div>
       
        <nav>
      
            <ul class="sf-menu">

                <li class="current"><asp:HyperLink ID="Demo" runat="server" NavigateUrl="~/Admin/Demo.aspx" CssClass="item">Home</asp:HyperLink>         </li>
               
                <li><asp:HyperLink ID="AddMenu" runat="server" NavigateUrl="~/Admin/Addmenu.aspx" CssClass="item">Addmenu</asp:HyperLink></li>
                <li><asp:HyperLink ID="submenu" runat="server" NavigateUrl="~/Admin/submenu.aspx" CssClass="item">submenu</asp:HyperLink> </li>
              
       
                <li>
                
                  <asp:HyperLink ID="createuser" runat="server" CssClass="item" NavigateUrl="~/Admin/creat.aspx">Create User</asp:HyperLink>
                </li>
                <li>
                    <asp:HyperLink ID="Guestbook" runat="server" CssClass="item" NavigateUrl="~/Admin/Guestbook.aspx">Guestbook</asp:HyperLink>
                </li>
                <li>
                <asp:HyperLink ID="ManageRole" runat="server" CssClass="item" NavigateUrl="~/Admin/ManageRole.aspx">Managerole</asp:HyperLink>
                </li>
            <li>
            <asp:HyperLink ID="RolebasedAuthorization" runat="server" NavigateUrl="~/Admin/RolebasedAuthorization.aspx" >RolebasedAuthorization</asp:HyperLink>
            </li>
               
                <li class="last"><asp:LinkButton ID="Lnlogout" runat="server" CssClass="item"
                        onclick="Lnlogout_Click" >Logout</asp:LinkButton></li>
            </ul>
        </nav>
  <a class="logo" href="#">King <span>Co.</span></a>
    </div>
    <div id="Div1">
     </div>        

        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
       
        </asp:ContentPlaceHolder>
        <footer>
    <div class="container_24">
        <div class="wrapper">
         <article class="grid_24">
             <div class="padding-right1">
                 <div class="border-bot p3"></div>
                </div>
            </article>
        </div>
        <div class="wrapper">
         <article class="grid_4">
             <strong>Pal Consult Co.  &copy; 2014 Privacy Policy</strong>
            </article>
            <article class="grid_4 prefix_2">
             USA - LOS ANGELES<br>901 East E Street Wilmington, CA 90744<br />
            </article>
            <article class="grid_5 prefix_1">
             E -mail: <a href="#">mail@demolink.org</a><br/>Mobile N0: +91-9123456789
            </article>
            <article class="grid_7 prefix_1">
             <!-- {%FOOTER_LINK} -->
            </article>
        </div>
    </div>       
</footer>
<div id="advanced"></div>
        </div>
        </form>
    </body>




Masterpage.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Services;
using System.Data;
using DAL;
using System.IO;
public partial class MasterPage : System.Web.UI.MasterPage
{
    Dal odal = new Dal();
    DataTable dt = new DataTable();
    string s = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AID"].ToString() == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            int id = Convert.ToInt32(Session["AID"].ToString());
            if (id == 1)
            {

            }
            else
            {
                checkuserrole();

            }
        }
    }
    protected void Lnlogout_Click(object sender, EventArgs e)
    {

        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Session.Clear();
        Session.Abandon();
        Session.RemoveAll();
        FormsAuthentication.SignOut();
        Response.Redirect("Login.aspx");



    }
    public void checkuserrole()
    {

        DataTable ddt = odal.selectbyquerydt("select pageid from usermaster where userid='" + Convert.ToInt16(Session["AID"].ToString()) + "'");
        if (ddt.Rows.Count == 0)
        {
        }
        else
        {
            s = ddt.Rows[0][0].ToString();
            if (s.IndexOf(",") == 0)
            {
                s = s.Remove(0, 1);
            }
            if (s != "")
            {
                char[] sep = { ',' };

                string[] filearray = s.Split(sep);
                string path = Path.GetFileName(Request.Url.AbsoluteUri.ToString());
                if (filearray.Contains(path))
                {

                }
                else
                {
                    if (path.ToString() == "demo.aspx" || path.ToString() == "success.aspx")
                    {

                    }
                    else
                    {
                        string strPreviousPage = "";
                        if (Request.UrlReferrer != null)
                        {
                            strPreviousPage = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
                        }
                        if (strPreviousPage == "")
                        {
                            Response.Redirect("~/Admin/Demo.aspx");
                        }
                    }
                }
                enablehyperlink(filearray);
            }

        }

    }
    public void enablehyperlink(string[] filearray)
    {
        checkhp(filearray, AddMenu);
        checkhp(filearray, submenu);
        checkhp(filearray, createuser);
        checkhp(filearray, Guestbook);
        checkhp(filearray, ManageRole);
        checkhp(filearray, RolebasedAuthorization);
    }
    public void checkhp(string[] filearray, HyperLink h)
    {
        if (filearray.Contains(Path.GetFileName(h.ID).ToString()))
        {
            h.Enabled = true;
            h.Visible = true;
        }
        else
        {
            h.Enabled = false;
            h.Visible = false;
            h.ToolTip = "Sorry,You Are Not Authorized To Open This Page";
            h.ForeColor = System.Drawing.Color.Red;
        }
    }

}




Comments

Popular posts from this blog

Periodic Restart Settings for Application Pool Recycling

Generate image ,pdf and Video Thumb using node js

How to receive JSON object in POST and GET using web api