A user-processing mechanism based on. Net.

Source: Internet
Author: User
Tags bool
User processing mechanism based on the. NET principal, Identity interface.
SiteIdentity.cs
Using System;
Using System.Text;
Using System.Collections;
Using System.Security.Cryptography;
Namespace Contextuser
{
<summary>
Summary description of the siteidentity.
</summary>
public class SiteIdentity:System.Security.Principal.IIdentity
{
private string UserName;
private string EmailAddress;
private int UserID;

Public siteidentity (String currentusername)
{
UserName = CurrentUserName;
EmailAddress = "test@msn.com";
UserID = 1;
}
public string AuthenticationType
{
Get
{
Return "Custom Authentication";
}
Set
{
Doing nothing
}
}
public bool IsAuthenticated
{
Get
{
Assumption:all instances of a siteidentity have already
been authenticated.
return true;
}
}
public string Name
{
Get
{
return userName;
}
}
public string EmailAddress
{
Get
{
return emailaddress;
}
}
public int UserID
{
Get
{
return UserID;
}
}
}
}
-----------------------------------------------------------------------------------------------
SitePrincipal.cs
Using System;
Using System.Collections;
Using System.Security;
Using System.Security.Cryptography;
Namespace Contextuser
{
<summary>
Summary description of the Siteprincipal.
</summary>
public class SitePrincipal:System.Security.Principal.IPrincipal
{
protected System.Security.Principal.IIdentity identity;
Protected ArrayList permissionlist;//License Dynamic array
Protected ArrayList rolelist;//role Dynamic Array
Public System.Security.Principal.IIdentity Identity
{
Get
{
return identity;
}
Set
{
identity = value;
}
}
Public Siteprincipal (string username)//constructor logic
{

identity = new Siteidentity (username);
Rolelist = new ArrayList ()///Add role array can be used to retrieve values from the database instead
Rolelist.add ("Admin");
Rolelist.add ("123");
Permissionlist = new ArrayList ();
Permissionlist.add (2);
Permissionlist = Datauser.geteffectivepermissionlist (UserID);
Rolelist = Datauser.getuserroles (UserID);
}
public static Siteprincipal Validatelogin (string Username, string password)/Verify Login
{
if (username = = "1111111" && password = = "1111111")//can be used to validate the substitution from the database
{
return new Siteprincipal (username);
}
Else
{
return null;
}

}
public bool IsInRole (string role)
{
return Rolelist.contains (role);
}
public bool Haspermission (int PermissionID)
{
Return Permissionlist.contains (PermissionID);
}
Public ArrayList Roles
{
Get
{
return rolelist;
}
}
Public ArrayList Permissions
{
Get
{
return permissionlist;
}
}
}
}

The following inheritance page is used for pages that require validation
BasePage.cs
Using System;
Using System.Web;
Using System.Web.UI;
Using System.Diagnostics;
Namespace Contextuser
{
<summary>
Summary description of the basepage.
</summary>
public class BasePage:System.Web.UI.Page
{
Public BasePage ()
{
}
protected override void OnInit (EventArgs e)
{
Base. OnInit (e);
This. Load + = new System.EventHandler (this. Basepage_load);
}
private void Basepage_load (object sender, System.EventArgs e)
{

if (Context.User.Identity.IsAuthenticated)
{
if (!) ( Context.User is Siteprincipal))
{
Response.Write ("Context.User.Identity.Is authenticated");
Siteprincipal NewUser = new Siteprincipal (Context.User.Identity.Name);
Context.User = NewUser;
Response.Write (Context.User.IsInRole ("123"));
}

}
}


}
}
--------------------------------------------------------------------------
Landing page
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.Security;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Namespace Contextuser
{
<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:ContextUser.BasePage
{
protected System.Web.UI.WebControls.TextBox username;
protected System.Web.UI.WebControls.TextBox pwd;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
private void Button1_Click (object sender, System.EventArgs e)
{
Siteprincipal principal = Siteprincipal.validatelogin (username. Text,pwd. Text);
if (principal==null)
{
Label1.Text = "Incorrect username or password";

}
Else
{
Context.User = Principal;
Formsauthentication.setauthcookie (username. Text, True);
if (Context.User.IsInRole ("123"))
{
Label2.Text = "User" + Context.User.Identity.Name + "belongs to admin Group";
}
Else
{
Label2.Text = "User" + Context.User.Identity.Name + "does not belong to admin group";
}
}
}
}
}
---------------------------------------------------
Validation page needs to inherit BasePage
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Namespace Contextuser
{
<summary>
Summary description of the _default.
</summary>
public class _default:contextuser.basepage
{
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load (object sender, System.EventArgs e)
{
if (! Context.User.Identity.IsAuthenticated | |
! ((Siteprincipal) context.user). Haspermission (2))
{
Page.Response.Redirect ("WebForm1.aspx");

}
Else
{
Page.Response.Write ("through");
}


}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
Base. OnInit (e)//------------------------------------------------------------notice it needs to be placed above.
InitializeComponent ();

}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}

Refer to some of the user's article and Wrox site advanced programming in this thanks!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.