Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


Proverb Web Service: Creating the Admin Page - Part 2

Add Comment
 

 
<div align="center"> <table class="outline" width="90%" border="0"> <tr> <td class="outline" width="40%"><b>Download File</b></td> <td class="outline" width="15%"><b>SDK</b></td> </tr> <tr> <td class="outline" width="40%"> <a class="wbox" href="../../file/proverbservice1.zip"> proverbservice1.zip</a> (15kb)</td> <td class="outline" width="15%">Beta2</td> <td class="outline" width="45%"> </tr> </table></div><p> <span class=wboxheado>Introduction</span><br> In continuation from the <a class="wbox" href="article.aspx?ArticleID=63&&TopicID=7">last part</a>, in this article I will construct the <b>Administration Page</b> for our Proverb Web Service. If you remember, in the last Article we built a Proverb Web Service, which supports 2 methods. One to view a random Proverb and another to add a Proverb for moderation. <br> The Administration page we build in this article will help the administrators of our web service to moderate (Accept / Reject)&nbsp; the Proverbs added by users to the web service.</p> <p><span class=wboxheado>Code</span></p> <p><b>1) adminpage.aspx</b> - The Proverb Web Service Administration page. I haven't made a very fancy page, you can add all the frills you want :)</p> <table width="100%" class="code"> <tr> <td width="100%"><pre>&lt;%@ Page Language=&quot;C#&quot; debug=&quot;true&quot; %&gt; &lt;%@ Import namespace=&quot;System.Data&quot; %&gt; &lt;%@ Import namespace=&quot;System.Data.OleDb&quot; %&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Proverb Web Service: Administration Page&lt;/title&gt; &lt;script runat=server&gt; public void Page_Load(object sender, EventArgs e) { <span class=cmt>//If Page is loaded for first time call the BuildGrid method</span> if(!IsPostBack) BuildGrid(); } <span class=cmt>//Method to Databind the Grids</span> protected void BuildGrid() { string conString=@&quot;Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=&quot;; conString+=Server.MapPath(&quot;.\\db\\proverb.mdb&quot;); string sqlString =&quot;SELECT * FROM Moderate&quot;; <span class=cmt>//Create a OleDb DataAdapter</span> OleDbDataAdapter modAdapter= new OleDbDataAdapter(sqlString,conString); DataSet modSet = new DataSet(); <span class=cmt>//Fill the DataSet</span> modAdapter.Fill(modSet,&quot;Moderate&quot;); <span class=cmt>//Databind the grid </span> ModGrid.DataSource = modSet.Tables[0].DefaultView; ModGrid.DataBind(); sqlString =&quot;SELECT * FROM Proverb&quot;; OleDbDataAdapter proAdapter= new OleDbDataAdapter(sqlString,conString); DataSet proSet = new DataSet(); <span class=cmt>//Fill the DataSet</span> proAdapter.Fill(proSet,&quot;Proverb&quot;); <span class=cmt>//Databind the grid</span> ProGrid.DataSource = proSet.Tables[0].DefaultView; ProGrid.DataBind(); } <span class=cmt>//Method called when Add/Remove button is clicked in the DataGrid</span> void ModGrid_Command(Object sender, DataGridCommandEventArgs e) { <span class=cmt>//Get the Proverb from the 4th cell in the table //Since array's are zero indexed we get the value at index 3!</span> TableCell proCell = e.Item.Cells[3]; string proVerb = proCell.Text; <span class=cmt>//Get the ID from the 3rd Cell</span> TableCell itemCell = e.Item.Cells[2]; string item = itemCell.Text; <span class=cmt>//Check if Accept Button was pressed</span> if (((Button)e.CommandSource).CommandName == &quot;Accept&quot;) { <span class=cmt>//Insert the proverb into the 'Proverb' table</span> string conString=@&quot;Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=&quot;; conString+=Server.MapPath(&quot;.\\db\\proverb.mdb&quot;); string sqlString =&quot;INSERT INTO proverb (Content) VALUES ('&quot;+proVerb+&quot;')&quot;; OleDbConnection proCon = new OleDbConnection(conString); OleDbCommand proCommand = new OleDbCommand(sqlString,proCon); proCon.Open(); proCommand.ExecuteNonQuery(); proCon.Close(); <span class=cmt>//Delete the proverb from 'Moderate' table</span> sqlString = &quot;DELETE FROM moderate WHERE ID=&quot;+item ; proCommand.CommandText=sqlString; proCon.Open(); proCommand.ExecuteNonQuery(); proCon.Close(); <span class=cmt>//Rebuild the grids </span> BuildGrid(); } else if(((Button)e.CommandSource).CommandName == &quot;Reject&quot;) { <span class=cmt>//Delete the proverb from the 'Moderate' table</span> string conString=@&quot;Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=&quot;; conString+=Server.MapPath(&quot;.\\db\\proverb.mdb&quot;); string sqlString = &quot;DELETE FROM moderate WHERE ID=&quot;+item ; OleDbConnection proCon = new OleDbConnection(conString); OleDbCommand proCommand = new OleDbCommand(sqlString,proCon); proCon.Open(); proCommand.ExecuteNonQuery(); proCon.Close(); <span class=cmt>//Rebuild the grids</span> BuildGrid(); } } <span class=cmt>//Method to add a new Proverb</span> protected void InsertNew(object sender, EventArgs e) { <span class=cmt>//Insert a new proverb into the 'Proverb' table</span> string conString=@&quot;Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=&quot;; conString+=Server.MapPath(&quot;.\\db\\proverb.mdb&quot;); string sqlString =&quot;INSERT INTO proverb (Content) VALUES ('&quot;+proText.Text+&quot;')&quot;; OleDbConnection proCon = new OleDbConnection(conString); OleDbCommand proCommand = new OleDbCommand(sqlString,proCon); proCon.Open(); proCommand.ExecuteNonQuery(); proCon.Close(); <span class=cmt>//Clear the TextBox</span> proText.Text=&quot;&quot;; <span class=cmt>//Rebuild the grids</span> BuildGrid(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form runat=&quot;Server&quot; &gt; &lt;div align=&quot;Center&quot;&gt; &lt;h2&gt;Proverb Web Service: Administration Page&lt;/h2&gt; Add a Proverb &lt;table border=&quot;1&quot;&gt; &lt;tr&gt;&lt;td&gt;Proverb&lt;/td&gt;&lt;td&gt;&lt;asp:TextBox id=&quot;proText&quot; runat=&quot;Server&quot; /&gt; &lt;asp:RequiredFieldValidator ControlToValidate=&quot;proText&quot; runat=&quot;server&quot;&gt;*&lt;/asp:RequiredFieldValidator&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td colspan=2&gt; &lt;asp:Button id=&quot;AddNew&quot; OnClick=&quot;InsertNew&quot; text=&quot;Add New&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;br&gt; Moderation Table &lt;asp:DataGrid id=&quot;ModGrid&quot; BorderColor=&quot;black&quot; BorderWidth=&quot;1&quot; CellPadding=&quot;3&quot; AutoGenerateColumns=&quot;false&quot; OnItemCommand=&quot;ModGrid_Command&quot; runat=&quot;server&quot;&gt; &lt;HeaderStyle BackColor=&quot;#00aaaa&quot;&gt; &lt;/HeaderStyle&gt; &lt;Columns&gt; &lt;asp:ButtonColumn HeaderText=&quot;Accept&quot; ButtonType=&quot;PushButton&quot; Text=&quot;Add&quot; CommandName=&quot;Accept&quot; /&gt; &lt;asp:ButtonColumn HeaderText=&quot;Reject&quot; ButtonType=&quot;PushButton&quot; Text=&quot;Remove&quot; CommandName=&quot;Reject&quot; /&gt; &lt;asp:BoundColumn HeaderText=&quot;Id&quot; DataField=&quot;ID&quot;/&gt; &lt;asp:BoundColumn HeaderText=&quot;Proverbs&quot; DataField=&quot;Content&quot;/&gt; &lt;asp:BoundColumn HeaderText=&quot;Date&quot; DataField=&quot;Dt&quot;/&gt; &lt;/Columns&gt; &lt;/asp:DataGrid&gt; &lt;br&gt; Active Table &lt;asp:Datagrid id=ProGrid BorderColor=&quot;black&quot; BorderWidth=&quot;1&quot; CellPadding=&quot;3&quot; runat=&quot;Server&quot; &gt; &lt;HeaderStyle BackColor=&quot;#00aaaa&quot;&gt; &lt;/HeaderStyle&gt; &lt;/asp:Datagrid&gt; Copyright &lt;a href=&quot;http://www.mastercsharp.com&quot;&gt;www.MasterCSharp.com&lt;/a&gt; all rights are reserved. &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre></td> </tr> </table> <p>Save this file as <b>adminpage.aspx</b> and you have the administration page ready!! Now copy this page into the '<b>ProverbService</b>' virtual directory you might have created in the last article. If you have hosted the Proverb Web Service in some other Virtual Directory, then copy this file into that directory. But remember, it has to be a Virtual Directory that hosts the Web Application.</p> <p><span class=wboxheado>Securing the Admin Page</span><br> Many of you might have got the hint that the page we have just created above is publicly accessible and totally beats it purpose. So we have to take some steps to restrict access to the page. There are many ways to do that, but for the sake of this example I will choose one of the easiest and that is <b>Form (Cookie) based authentication</b> provided by ASP.NET.</p> <p><span class=wboxhead>Step 1: Login Page - login.aspx</span><br> Since we are using Form Based Authentication for our application, we need to create another page that will redirect all unauthorized to a page to login.<br> <b>2) login.aspx</b> - The login page.</p> <table border="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" class="code"> <tr> <td width="100%"><pre>&lt;%@ Page language=C# %&gt; &lt;%@ Import Namespace=&quot;System.Web.Security &quot; %&gt; &lt;html&gt; &lt;script language=&quot;C#&quot; runat=server&gt; void Login_Click(Object sender, EventArgs E) { <span class=cmt>//Check if the Email and Password values correspond. //I have hard-coded the values... you can implement your own logic </span> if ((UserEmail.Value == &quot;admin@mastercsharp.com&quot;) &amp;&amp; (UserPass.Value == &quot;abcd1234&quot;)) { <span class=cmt>//If credentials are proper, Authenticate the use and set the cookie</span> FormsAuthentication.RedirectFromLoginPage(UserEmail.Value, PersistCookie.Checked); } else { Msg.Text = &quot;Invalid Credentials: Please try again&quot;; } } &lt;/script&gt; &lt;body&gt; &lt;form runat=server&gt; &lt;h3&gt;&lt;font face=&quot;Verdana&quot;&gt;Login Page&lt;/font&gt;&lt;/h3&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Email:&lt;/td&gt; &lt;td&gt;&lt;input id=&quot;UserEmail&quot; type=&quot;text&quot; runat=server/&gt;&lt;/td&gt; &lt;td&gt;&lt;ASP:RequiredFieldValidator ControlToValidate=&quot;UserEmail&quot; Display=&quot;Static&quot; ErrorMessage=&quot;*&quot; runat=server/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Password:&lt;/td&gt; &lt;td&gt;&lt;input id=&quot;UserPass&quot; type=password runat=server/&gt;&lt;/td&gt; &lt;td&gt;&lt;ASP:RequiredFieldValidator ControlToValidate=&quot;UserPass&quot; Display=&quot;Static&quot; ErrorMessage=&quot;*&quot; runat=server/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Persistent Cookie:&lt;/td&gt; &lt;td&gt;&lt;ASP:CheckBox id=PersistCookie runat=&quot;server&quot; /&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;asp:button text=&quot;Login&quot; OnClick=&quot;Login_Click&quot; runat=server/&gt; &lt;br&gt; &lt;asp:Label id=&quot;Msg&quot; ForeColor=&quot;red&quot; Font-Name=&quot;Verdana&quot; Font-Size=&quot;10&quot; runat=server /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre></td> </tr> </table> <p>As you can see above, the login page is very simple. I have hard-coded the values for e-mail and password but for a real world solution you can implement a database checking. Save this page as login.aspx and copy it into the same 'ProverbService' Virtual Directory.<p><span class=wboxhead>Step 2: Application Configuration - Web.Config</span><br> As the final step to secure the admin page, we have to inform the ASP.NET runtime to secure the AdminPage.aspx file and only allow authenticated users to view the page. The ASP.NET pick's up these settings from the Xml formatted <b>Web.Config</b> file. For more information on the Web.Config file see the ASP.NET Documentation. I would just add that there can only be one Web.Config per Web Application hosted in a Virtual Directory. Again, please note that your application has to reside in a Virtual Directory (not a normal directory) or you will start getting weird errors!<br> Explaining the different sections of this file will take up a series of articles on its own, you can look into the <b>ASP.NET Quick Start</b> for more information.<p>&nbsp;<table border="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" class="code"> <tr> <td width="100%"><pre>&lt;configuration&gt; &lt;system.web&gt; &lt;authentication mode=&quot;Forms&quot;&gt; &lt;forms name=&quot;ProverbService&quot; loginUrl=&quot;login.aspx&quot; protection=&quot;All&quot; path=&quot;/&quot; /&gt; &lt;/authentication&gt; &lt;/system.web&gt; &lt;location path=&quot;adminpage.aspx&quot;&gt; &lt;system.web&gt; &lt;authorization&gt; &lt;deny users=&quot;?&quot; /&gt; &lt;/authorization&gt; &lt;/system.web&gt; &lt;/location&gt; &lt;/configuration&gt;</pre></td> </tr> </table> <p>Save this file as Web.Config and place it into the 'ProverbService' Virtual Directory. Once that's done, your page is secure!<p><span class=wboxheado>Calling the Page</span><br> Once you have setup everything, its testing time!! Fire up your favorite browser and enter the url to the Admin Page i.e. http://localhost/provebservice/adminpage.aspx . Your browser should automatically redirect you to 'login.aspx' page to enter your credentials. If you enter the proper credentials you will be re-directed back to this page, automatically!!<p><span class=wboxheado>Conclusion</span><br> In this part of the Proverb Web Service, we learnt how to build the admin page for our service, as well as we learned how to secure the page using ASP.NET Form based authentication. With this, we finish the server deployment of our web service, next article onwards we will see how to build various Clients for our Web Service.

Comments

Add Comment