SoFunction
Updated on 2025-03-07

Forms authentication to avoid duplicate wheel

Question: Everyone says that using formms verification cannot get more information about the current logged-in user besides the username. After some small experiments, the userdata that comes with formms can display the world for us. Let’s record my operation steps.
step 1: Key configuration:
Configuration
Copy the codeThe code is as follows:

<!--
You can configure it to use the <authentication> section
Identify the entry user
Secure authentication mode.
-->
<authentication mode="Forms">
<forms loginUrl="" defaultUrl=""
name=".ztinfozero" path="/Manager"
slidingExpiration="true" timeout="10"></forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>

step 2: Construct SiteUser Model
Copy the codeThe code is as follows:

TopicUser Model
[Serializable]
public class TopicUser
{
public TopicUser() { }
model#region model
private System.Int32 _autoID;
/**//// <summary>
///
/// </summary>
public System.Int32 autoID
{
get { return _autoID; }
set { _autoID = value; }
}
private _UserName;
/**//// <summary>
/// username
/// </summary>
public UserName
{
get { return _UserName; }
set { _UserName = value; }
}
private _UserChName;
/**//// <summary>
/// Real name
/// </summary>
public UserChName
{
get { return _UserChName; }
set { _UserChName = value; }
}
private _UserPass;
/**//// <summary>
///
/// </summary>
public UserPass
{
get { return _UserPass; }
set { _UserPass = value; }
}
private _DepartMent;
/**//// <summary>
///
/// </summary>
public DepartMent
{
get { return _DepartMent; }
set { _DepartMent = value; }
}
private _Duty;
/**//// <summary>
///
/// </summary>
public Duty
{
get { return _Duty; }
set { _Duty = value; }
}
private System.Int32 _UserPermit;
/**//// <summary>
///
/// </summary>
public System.Int32 UserPermit
{
get { return _UserPermit; }
set { _UserPermit = value; }
}
private System.Int32 _Status;
/**//// <summary>
///
/// </summary>
public System.Int32 Status
{
get { return _Status; }
set { _Status = value; }
}
#endregion
}

step 3: Create user login code:

Database - User login method
Copy the codeThe code is as follows:

public TopicUser UserLogon(string username, string pass) {
string proc = "dbo.infozero_Proc_userLogOn";
Database db = ;
DbCommand cmd = (proc);
(cmd, "@username", , username);
(cmd, "@userpass", , pass);
(cmd, "@result", DbType.Int32, 4);
DataSet ds = (cmd);
TopicUser user = null;
int result = 0;
if (((cmd, "@result").ToString(), out result) )
user = tableToUser([0]);
return user;
}
#region table to user
private TopicUser tableToUser(DataTable dt) {
TopicUser model = null;
if ( > 0) {
model = new TopicUser();
DataRow dr = [0];
int aid = 0;
(dr["autoID"].ToString(), out aid );
= aid;
= dr["UserName"].ToString();
= dr["UserChName"].ToString();
= dr["UserPass"].ToString();
= dr["DepartMent"].ToString();
= dr["Duty"].ToString();
if (dr["UserPermit"].ToString() != "")
{
= (dr["UserPermit"].ToString());
}
if (dr["Status"].ToString() != "")
{
= (dr["Status"].ToString());
}
}
return model;
}
#endregion

step 4: Create a login page:

Code
Copy the codeThe code is as follows:

protected void btnOK_Click(object sender, EventArgs e)
{
string username = ();
string pass = ();
if (!(username)) {
if (!(pass)) {
b = new ();
user = (username, pass);
if (user != null) {
//roles , userid | userchname
string userdata = ("{0},{1}|{2}",
, , );
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
, username, , (2),
true, userdata);
string encticket = (ticket);
HttpCookie cookie = new HttpCookie(
, encticket);
(cookie);
("");
}
}
}
}

step 5: Add the Application_AuthenticateRequest event to set the information of the currently logged-in user:
Copy the codeThe code is as follows:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpCookie cookie = [];
if (cookie != null) {
FormsAuthenticationTicket ticket = ();
if (ticket != null) {
string[] roles = (',');
FormsIdentity id = new FormsIdentity(ticket);
principal = new GenericPrincipal(id, roles);
= principal;
}
}
}

step 6: How to get the information of the currently logged in user
Copy the codeThe code is as follows:

public static TopicUser CurrentUser {
get {
user = new ();
FormsIdentity identity = as FormsIdentity;
FormsAuthenticationTicket ticket = ;
string userdata = ; //Get the custom UserData string
if (!(userdata)) {
if ((',') > 0 && ('|') > 0)
{
//roles , userid | userchname
string uinfo = (',')[1];
string[] u = ('|');
int uid = 0;
(u[0], out uid);
= uid;
= u[1];
= ;
}
}
return user;
}
}

From this, the ID of the currently logged in user is ; the real name is: ;
Determine whether the current user's role is an administrator: ("1"); // 1 is an administrator
How to log out of the current login:

Copy the codeThe code is as follows:

protected void Page_Load(object sender, EventArgs e)
{
();
("<script>='';</script>");
();
}

At this point, the authentication is completed. We don't have to worry about stacking codes around whether users are logged in or not.