📄 siteprincipal.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
namespace RolesBasedAthentication
{
public class SitePrincipal: System.Security.Principal.IPrincipal
{
protected System.Security.Principal.IIdentity identity;
protected ArrayList roleList;
public SitePrincipal( string email )
{
identity = new SiteIdentity(email);
roleList = User.GetUserRoles( ((SiteIdentity)identity).UserID );
}
public SitePrincipal( int userID )
{
identity = new SiteIdentity(userID);
roleList = User.GetUserRoles( ((SiteIdentity)identity).UserID );
}
public static SitePrincipal ValidateLogin(string email, string password)
{
int userID;
string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("ValidateLogin", connection);
command.Parameters.Add("@Email", email);
command.Parameters.Add("@Password", password);
// add return value parameter
command.Parameters.Add( new SqlParameter ( "ReturnValue",SqlDbType.Int,4,
ParameterDirection.ReturnValue,false,0,0,string.Empty,DataRowVersion.Default,null ));
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
userID = (int)command.Parameters["ReturnValue"].Value;
command.Dispose();
connection.Close();
return (userID == 0 ? null : new SitePrincipal(userID));
}
public bool IsInRole(string role)
{
return roleList.Contains( role );
}
// Properties
public System.Security.Principal.IIdentity Identity
{
get { return identity; }
set { identity = value; }
}
public ArrayList Roles
{
get { return roleList; }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -