I'm stuck with loginstatus.I'm doing a project with different access rules, it has different roles like Admin, Executives, Director I have added loginview and login status to my master page. When i login as Admin, I want it to show "Logged in as Admin" or
When i login as Director, I want it to show "Logged in as Director" . I'm using sql server and not membership provider. I'm not able to do this. Can you pls help me with this?
Sorry, just saw your reply.. I'm a beginner.. Can you please help me how to write the code or show me with the code hw to do it..
I'm doing a project which has different access rules, like Admin has all right, Customers & Users can do only certain things.. When I login as Admin, it should show something like "You are Logged in as Admin" or When I login as Customer, it should show something
like "You are Logged in as Customer" .. I don't want to use mebership provider.. I'm using SQL Server.. I have 2 tables, One is User and other is Groups.. I want to display loginstatus along in Master Page.. Loin control i have written in content page..
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ClientIdTextBox" CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
<asp:Label ID="PasswordLabel" runat="server">Password:</asp:Label>
<p> <a href="Account/ChangePassword.aspx">ChangePassword </a> </p> <p> Not a member yet? <a href="Account/Register.aspx"> <br /> Register Here</a> <br /> <br /> </p> </fieldset> </asp:Content>
Code Behind
---------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.LoginButton.Click += new System.EventHandler(this.LoginButton_Click);
string currentUser = Page.User.Identity.Name;
}
private bool ValidateUser(string userName, string passWord)
{
SqlConnection conn;
SqlCommand cmd;
string lookupPassword = null;
string lookupGroup = "";
//string str;
// Check for invalid userName.
// userName must not be null and must be between 1 and 15 characters.
if ((null == userName) || (0 == userName.Length) || (userName.Length > 15))
{
System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.");
return false;
}
// Check for invalid passWord.
// passWord must not be null and must be between 1 and 25 characters.
if ((null == passWord) || (0 == passWord.Length) || (passWord.Length > 25))
{
System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.");
return false;
}
try
{
StringBuilder strSql = new StringBuilder();
// Consult with your SQL Server administrator for an appropriate connection
// string to use to connect to your local SQL Server.
conn = new SqlConnection(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;database=UserDetails");
conn.Open();
//Create SqlCommand to select pwd field from users table given supplied userName.
cmd = new SqlCommand("Select Users.UserName,Users.Password, Groups.GroupName, Groups.GroupId from Groups INNER JOIN Users ON Groups.GroupID=Users.USERID WHERE Users.UserName=@userName", conn);
cmd.Parameters.Add("@userName", System.Data.SqlDbType.VarChar, 25);
cmd.Parameters["@userName"].Value = userName;
// Execute command and fetch pwd field into lookupPassword string.
SqlDataReader rd = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rd);
lookupPassword = dt.Rows[0]["Password"].ToString();
lookupGroup = dt.Rows[0]["GroupName"].ToString();
//// Cleanup command and connection objects.
cmd.Dispose();
conn.Close();
conn.Dispose();
if ((lookupGroup) == "Clients")
{
Response.Redirect("Director.aspx", true);
}
else if ((lookupGroup) == "Admin")
{
Response.Redirect("Admin.aspx", true);
}
}
catch (Exception ex)
{
// Add error handling here for debugging.
// This error message should not be sent back to the caller.
System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
}
// If no password found, return false.
if (null == lookupPassword)
{
// You could write failed login attempts here to event log for additional security.
return false;
}
// Compare lookupPassword and input passWord, using a case-sensitive comparison.
return (0 == string.Compare(lookupPassword, passWord, false));
}
protected void LoginButton_Click(object sender, EventArgs e)
{
if (!ValidateUser(ClientIdTextBox.Value, PasswordTextBox.Value))
{
Response.Redirect("StartPage.aspx", true);
}
}
}
Priya123gill
Member
51 Points
35 Posts
Showing ALL results when user does not select any option from drop down
Jun 15, 2011 01:19 AM|LINK
I'm stuck with loginstatus.I'm doing a project with different access rules, it has different roles like Admin, Executives, Director I have added loginview and login status to my master page. When i login as Admin, I want it to show "Logged in as Admin" or When i login as Director, I want it to show "Logged in as Director" . I'm using sql server and not membership provider. I'm not able to do this. Can you pls help me with this?
Thanks,
Priya
Hua-Jun Li -...
All-Star
75950 Points
5608 Posts
Re: Showing ALL results when user does not select any option from drop down
Jun 21, 2011 08:37 AM|LINK
Hi,
You should judge the user's indentity, then assgin the different the logintext for it.
void Button1_Click(Object sender, EventArgs e) {
//get the user's role.
LoginStatus1.LoginText = "Logged in as Admin.";
}
Please check the following link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.loginstatus.logintext.aspx
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Priya123gill
Member
51 Points
35 Posts
Re: Showing ALL results when user does not select any option from drop down
Jun 22, 2011 05:46 PM|LINK
Sorry, just saw your reply.. I'm a beginner.. Can you please help me how to write the code or show me with the code hw to do it..
I'm doing a project which has different access rules, like Admin has all right, Customers & Users can do only certain things.. When I login as Admin, it should show something like "You are Logged in as Admin" or When I login as Customer, it should show something like "You are Logged in as Customer" .. I don't want to use mebership provider.. I'm using SQL Server.. I have 2 tables, One is User and other is Groups.. I want to display loginstatus along in Master Page.. Loin control i have written in content page..
My Master
<asp:LoginView ID="MasterLoginView" runat="server">
<LoggedInTemplate>
Welcome:
<asp:LoginName ID="MasterLoginName" runat="server" />
</LoggedInTemplate>
<AnonymousTemplate>
<a href="StartPage.aspx">Click here to Login</a><br /><br />
New:
<a href="Account/Register.aspx"> Register here </a>
</AnonymousTemplate>
</asp:LoginView>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<asp:ContentPlaceHolder>
</div>
<br />
<br />
</div>
</div>
<!-- end #login -->
<!--end #updates -->
</div>
Content Page
------------
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <fieldset> <legend>Sign-In</legend> <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" ValidationGroup="LoginUserValidationGroup" /> <br />
<asp:Label ID="ClientIdLabel" runat="server">User ID:</asp:Label>
<input id="ClientIdTextBox" runat="server"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ClientIdTextBox" CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> <asp:Label ID="PasswordLabel" runat="server">Password:</asp:Label>
<input id="PasswordTextBox" type="password" runat="server"/> <br />
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="PasswordTextBox" CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> <p> <asp:CheckBox ID="RememberMeChkBox" runat="server" />
<asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMeChkBox" CssClass="inline">Remember My Sign-In </asp:Label> </p>
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Sign In" ValidationGroup="LoginUserValidationGroup" PostBackUrl="~/StartPage.aspx" OnClick="LoginButton_Click" />
<p> <a href="Account/ChangePassword.aspx">ChangePassword </a> </p> <p> Not a member yet? <a href="Account/Register.aspx"> <br /> Register Here</a> <br /> <br /> </p> </fieldset> </asp:Content>
Code Behind
---------------
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.Security; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.Text; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { this.LoginButton.Click += new System.EventHandler(this.LoginButton_Click); string currentUser = Page.User.Identity.Name; } private bool ValidateUser(string userName, string passWord) { SqlConnection conn; SqlCommand cmd; string lookupPassword = null; string lookupGroup = ""; //string str; // Check for invalid userName. // userName must not be null and must be between 1 and 15 characters. if ((null == userName) || (0 == userName.Length) || (userName.Length > 15)) { System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed."); return false; } // Check for invalid passWord. // passWord must not be null and must be between 1 and 25 characters. if ((null == passWord) || (0 == passWord.Length) || (passWord.Length > 25)) { System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed."); return false; } try { StringBuilder strSql = new StringBuilder(); // Consult with your SQL Server administrator for an appropriate connection // string to use to connect to your local SQL Server. conn = new SqlConnection(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;database=UserDetails"); conn.Open(); //Create SqlCommand to select pwd field from users table given supplied userName. cmd = new SqlCommand("Select Users.UserName,Users.Password, Groups.GroupName, Groups.GroupId from Groups INNER JOIN Users ON Groups.GroupID=Users.USERID WHERE Users.UserName=@userName", conn); cmd.Parameters.Add("@userName", System.Data.SqlDbType.VarChar, 25); cmd.Parameters["@userName"].Value = userName; // Execute command and fetch pwd field into lookupPassword string. SqlDataReader rd = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(rd); lookupPassword = dt.Rows[0]["Password"].ToString(); lookupGroup = dt.Rows[0]["GroupName"].ToString(); //// Cleanup command and connection objects. cmd.Dispose(); conn.Close(); conn.Dispose(); if ((lookupGroup) == "Clients") { Response.Redirect("Director.aspx", true); } else if ((lookupGroup) == "Admin") { Response.Redirect("Admin.aspx", true); } } catch (Exception ex) { // Add error handling here for debugging. // This error message should not be sent back to the caller. System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message); } // If no password found, return false. if (null == lookupPassword) { // You could write failed login attempts here to event log for additional security. return false; } // Compare lookupPassword and input passWord, using a case-sensitive comparison. return (0 == string.Compare(lookupPassword, passWord, false)); } protected void LoginButton_Click(object sender, EventArgs e) { if (!ValidateUser(ClientIdTextBox.Value, PasswordTextBox.Value)) { Response.Redirect("StartPage.aspx", true); } } }User Table
UserID UserName Password
Groups Table
GroupID UserID GroupName