📄 sqlprofileprovider.cs
字号:
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
{
SqlParameter [] args = new SqlParameter[1];
args[0] = CreateInputParam("@InactiveSinceDate", SqlDbType.DateTime, userInactiveSinceDate.ToUniversalTime());
return GetProfilesForQuery(args, authenticationOption, pageIndex, pageSize, out totalRecords);
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
SecUtility.CheckParameter(ref usernameToMatch, true, true, false, 256, "username");
SqlParameter[] args = new SqlParameter[1];
args[0] = CreateInputParam("@UserNameToMatch", SqlDbType.NVarChar, usernameToMatch);
return GetProfilesForQuery(args, authenticationOption, pageIndex, pageSize, out totalRecords);
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
{
SecUtility.CheckParameter(ref usernameToMatch, true, true, false, 256, "username");
SqlParameter[] args = new SqlParameter[2];
args[0] = CreateInputParam("@UserNameToMatch", SqlDbType.NVarChar, usernameToMatch);
args[1] = CreateInputParam("@InactiveSinceDate", SqlDbType.DateTime, userInactiveSinceDate.ToUniversalTime());
return GetProfilesForQuery(args, authenticationOption, pageIndex, pageSize, out totalRecords);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Remove CAS in sample: [SecurityPermission(SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
private static void ParseDataFromDB(string[] names, string values, byte[] buf, SettingsPropertyValueCollection properties)
{
if (names == null || values == null || buf == null || properties == null)
return;
try {
for (int iter = 0; iter < names.Length / 4; iter++) {
string name = names[iter * 4];
SettingsPropertyValue pp = properties[name];
if (pp == null) // property not found
continue;
int startPos = Int32.Parse(names[iter * 4 + 2], CultureInfo.InvariantCulture);
int length = Int32.Parse(names[iter * 4 + 3], CultureInfo.InvariantCulture);
if (length == -1 && !pp.Property.PropertyType.IsValueType) // Null Value
{
pp.PropertyValue = null;
pp.IsDirty = false;
pp.Deserialized = true;
}
if (names[iter * 4 + 1] == "S" && startPos >= 0 && length > 0 && values.Length >= startPos + length) {
pp.SerializedValue = values.Substring(startPos, length);
}
if (names[iter * 4 + 1] == "B" && startPos >= 0 && length > 0 && buf.Length >= startPos + length) {
byte[] buf2 = new byte[length];
Buffer.BlockCopy(buf, startPos, buf2, 0, length);
pp.SerializedValue = buf2;
}
}
} catch { // Eat exceptions
}
}
// Remove CAS in sample: [SecurityPermission(SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
private static void PrepareDataForSaving(ref string allNames, ref string allValues, ref byte[] buf, bool binarySupported, SettingsPropertyValueCollection properties, bool userIsAuthenticated)
{
StringBuilder names = new StringBuilder();
StringBuilder values = new StringBuilder();
MemoryStream ms = (binarySupported ? new System.IO.MemoryStream() : null);
try {
try {
bool anyItemsToSave = false;
foreach (SettingsPropertyValue pp in properties) {
if (pp.IsDirty) {
if (!userIsAuthenticated) {
bool allowAnonymous = (bool)pp.Property.Attributes["AllowAnonymous"];
if (!allowAnonymous)
continue;
}
anyItemsToSave = true;
break;
}
}
if (!anyItemsToSave)
return;
foreach (SettingsPropertyValue pp in properties) {
if (!userIsAuthenticated) {
bool allowAnonymous = (bool)pp.Property.Attributes["AllowAnonymous"];
if (!allowAnonymous)
continue;
}
if (!pp.IsDirty && pp.UsingDefaultValue) // Not fetched from DB and not written to
continue;
int len = 0, startPos = 0;
string propValue = null;
if (pp.Deserialized && pp.PropertyValue == null) // is value null?
{
len = -1;
} else {
object sVal = pp.SerializedValue;
if (sVal == null) {
len = -1;
} else {
if (!(sVal is string) && !binarySupported) {
sVal = Convert.ToBase64String((byte[])sVal);
}
if (sVal is string) {
propValue = (string)sVal;
len = propValue.Length;
startPos = values.Length;
} else {
byte[] b2 = (byte[])sVal;
startPos = (int)ms.Position;
ms.Write(b2, 0, b2.Length);
ms.Position = startPos + b2.Length;
len = b2.Length;
}
}
}
names.Append(pp.Name + ":" + ((propValue != null) ? "S" : "B") +
":" + startPos.ToString(CultureInfo.InvariantCulture) + ":" + len.ToString(CultureInfo.InvariantCulture) + ":");
if (propValue != null)
values.Append(propValue);
}
if (binarySupported) {
buf = ms.ToArray();
}
} finally {
if (ms != null)
ms.Close();
}
} catch {
throw;
}
allNames = names.ToString();
allValues = values.ToString();
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
private ProfileInfoCollection GetProfilesForQuery(SqlParameter [] args, ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
{
if ( pageIndex < 0 )
throw new ArgumentException(SR.GetString(SR.PageIndex_bad), "pageIndex");
if ( pageSize < 1 )
throw new ArgumentException(SR.GetString(SR.PageSize_bad), "pageSize");
long upperBound = (long)pageIndex * pageSize + pageSize - 1;
if ( upperBound > Int32.MaxValue )
{
throw new ArgumentException(SR.GetString(SR.PageIndex_PageSize_bad), "pageIndex and pageSize");
}
try {
SqlConnectionHolder holder = null;
SqlDataReader reader = null;
try {
holder = SqlConnectionHelper.GetConnection(_sqlConnectionString, true);
CheckSchemaVersion( holder.Connection );
SqlCommand cmd = new SqlCommand("dbo.aspnet_Profile_GetProfiles", holder.Connection);
cmd.CommandTimeout = CommandTimeout;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(CreateInputParam("@ApplicationName", SqlDbType.NVarChar, ApplicationName));
cmd.Parameters.Add(CreateInputParam("@ProfileAuthOptions", SqlDbType.Int, (int) authenticationOption));
cmd.Parameters.Add(CreateInputParam("@PageIndex", SqlDbType.Int, pageIndex));
cmd.Parameters.Add(CreateInputParam("@PageSize", SqlDbType.Int, pageSize));
foreach (SqlParameter arg in args)
cmd.Parameters.Add(arg);
reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
ProfileInfoCollection profiles = new ProfileInfoCollection();
while (reader.Read())
{
string username;
DateTime dtLastActivity, dtLastUpdated;
bool isAnon;
username = reader.GetString(0);
isAnon = reader.GetBoolean(1);
dtLastActivity = DateTime.SpecifyKind(reader.GetDateTime(2), DateTimeKind.Utc);
dtLastUpdated = DateTime.SpecifyKind(reader.GetDateTime(3), DateTimeKind.Utc);
int size = reader.GetInt32(4);
profiles.Add(new ProfileInfo(username, isAnon, dtLastActivity, dtLastUpdated, size));
}
totalRecords = profiles.Count;
if (reader.NextResult())
if (reader.Read())
totalRecords = reader.GetInt32(0);
return profiles;
} finally {
if (reader != null)
reader.Close();
if( holder != null )
{
holder.Close();
holder = null;
}
}
}
catch {
throw;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -