导航
  • 主页
  • ASP
  • .NET Framework
  • Web Services
  • VB
  • VC
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • LINQ
  • 问答

ProfileProvider 重写问题。。。

bhtfg538 2008-08-30 11:48:32
我自己定义了一个
MyProfile 类继承自ProfileProvider
Config 是这样定义的
<anonymousIdentification enabled="true"/>
<profile defaultProvider="ProfileConnection">
<providers>
<clear/>
<add name="ProfileConnection" connectionStringName="ProfileConnection" type="Profile.WaWaProfileProvider" applicationName="WaWa Beta 1.0"/>
</providers>
<properties>
<clear/>
<add name="AccountInfo" type="Models.Account" provider="ProfileConnection" allowAnonymous="true" />
</properties>
为什么 IDE 里面就是不提示ProfileCommon 下的Profile 属性
ProfileConnection 是<connectionStrings>下的 某连接
Models.Account是Model 下的Account 实体

连接没有问题

不知道是什么原因

贴出自己的Profile
public sealed class WaWaProfileProvider : ProfileProvider
{
// Get an instance of the ProfileDAL using the ProfileDALFactory,though you can Config the Web.Config to Change Reflection
private static readonly IProfileDAL.IProfileProvider dal = ProfileFactory.DataAccess.CreatePetShopProfileProvider();
// Private members
private const string ERR_INVALID_PARAMETER = "Invalid Profile parameter:";
private const string PROFILE_ACCOUNT = "Account";//Account Information
private static string applicationName = "WaWa Beta 1.0";
#region "ProfileProvider"
/// <summary>
/// Retrieve Application Name
/// </summary>
public override string ApplicationName
{
get
{
return applicationName;
}
set
{
applicationName = value;
}
}
/// <summary>
/// Initial Function From ProfileBase
/// </summary>
/// <param name="name">Set a Friendly Name</param>
/// <param name="config">Name/Value Collection</param>
public override void Initialize(string name, NameValueCollection config)
{
if (config == null)
throw new ArgumentNullException("config");

if (string.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description", "WaWa Custom Profile Provider");
}

if (string.IsNullOrEmpty(name))
name = "WaWaProfileProvider";

if (config["applicationName"] != null && !string.IsNullOrEmpty(config["applicationName"].Trim()))
applicationName = config["applicationName"];

base.Initialize(name, config);

}
/// <summary>
/// Retrieve SettingsPropertyValueCollection of Profile Property Value
/// </summary>
/// <param name="context">SettingsContext(Hashtable instance,Get a key/value Collection for Profile R/W)</param>
/// <param name="collection"></param>
/// <returns></returns>
public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection collection)
{
string username = (string)context["UserName"];
bool isAuthenticated = (bool)context["IsAuthenticated"];

SettingsPropertyValueCollection svc = new SettingsPropertyValueCollection();

foreach(SettingsProperty prop in collection) {
SettingsPropertyValue pv = new SettingsPropertyValue(prop);

switch(pv.Property.Name) {
case PROFILE_ACCOUNT:
if(isAuthenticated)
pv.PropertyValue = GetAccountInfo(username);
break;
default:
throw new ApplicationException(ERR_INVALID_PARAMETER + " name.");
}
svc.Add(pv);
}
return svc;
}
private object GetAccountInfo(string username)
{
return dal.GetAccountInfo(username, applicationName);
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
{

string username = (string)context["UserName"];
CheckUserName(username);
bool isAuthenticated = (bool)context["IsAuthenticated"];
int uniqueID = dal.GetUniqueID(username, isAuthenticated, false, ApplicationName);
if (uniqueID == 0)
uniqueID = dal.CreateProfileForUser(username, isAuthenticated, ApplicationName);

foreach (SettingsPropertyValue pv in collection)
{
if (pv.PropertyValue != null)
{
switch (pv.Property.Name)
{
case PROFILE_ACCOUNT:
if (isAuthenticated)
SetAccountInfo(uniqueID, (Account)pv.PropertyValue);
break;
default:
throw new ApplicationException(ERR_INVALID_PARAMETER + " name.");
}
}
}
}
private static void SetAccountInfo(int uniqueID, Account account)
{
dal.SetAccountInfo(uniqueID, account);
}
private static bool DeleteProfile(string username)
{
CheckUserName(username);
return dal.DeleteProfile(username, applicationName);
}

private static void CheckUserName(string userName)
{
if (string.IsNullOrEmpty(userName) || userName.Length > 256 || userName.IndexOf(",") > 0)
throw new ApplicationException(ERR_INVALID_PARAMETER + " user name.");
}
/// <summary>
/// Delete Profiles
/// </summary>
/// <param name="profiles">A System.Web.Profile.ProfileInfoCollection of information about profiles that are to be deleted.</param>
/// <returns>The number of profiles deleted from the data source.</returns>
public override int DeleteProfiles(ProfileInfoCollection profiles)
{
int deleteCount = 0;

foreach (ProfileInfo p in profiles)
if (DeleteProfile(p.UserName))
deleteCount++;

return deleteCount;
}

/// <summary>
///
/// </summary>
/// <param name="usernames"></param>
/// <returns></returns>
public override int DeleteProfiles(string[] usernames)
{
int deleteCount = 0;
foreach (string user in usernames)
if (DeleteProfile(user))
deleteCount++;

return deleteCount;
}


...全文
192 点赞 收藏 14
写回复
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
huaizuo2010 2011-07-14
跟 PETSHOP 4 代码很相似,可以参考那个
回复
sevenisme777 2009-03-10
跟 PETSHOP 4 代码很相似,可以参考那个
回复
bhtfg538 2008-09-01
为什么 Profile 本地数据连接 可以
链接服务器就有问题了
连接没有出问题

其他都没有动
bin 下面也有
回复
YYYYYYYYY-X 2008-08-31
.net???~
回复
banditi225 2008-08-30
是方法的提示出不来吗
回复
up
回复
bhtfg538 2008-08-30
谢谢你了
问题还没解决
郁闷了好一阵子
还不知道是什么错误啊
回复
lifelz888 2008-08-30
看看,有点头痛呵.
回复
bhtfg538 2008-08-30
大家来看下帮帮忙
回复
bhtfg538 2008-08-30
UP 一个
回复
bhtfg538 2008-08-30
/// <summary>
/// profiles deleted from the data source for the special authentication options and datetime
/// </summary>
/// <param name="authenticationOption">One of the System.Web.Profile.ProfileAuthenticationOption values, specifying whether anonymous, authenticated, or both types of profiles are deleted.</param>
/// <param name="userInactiveSinceDate">A System.DateTime that identifies which user profiles are considered inactive. If the System.Web.Profile.ProfileInfo.LastActivityDate value of a user profile occurs on or before this date and time, the profile is considered inactive.</param>
/// <returns>The number of profiles deleted from the data source.</returns>
public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
{
string[] userArray = new string[0];
dal.GetInactiveProfiles((int)authenticationOption, userInactiveSinceDate, ApplicationName).CopyTo(userArray, 0);

return DeleteProfiles(userArray);
}
public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{

CheckParameters(pageIndex, pageSize);

return GetProfileInfo(authenticationOption, usernameToMatch, null, pageIndex, pageSize, out totalRecords);
}

private static void CheckParameters(int pageIndex, int pageSize)
{
if (pageIndex < 1 || pageSize < 1)
throw new ApplicationException(ERR_INVALID_PARAMETER + " page index.");
}

private static ProfileInfoCollection GetProfileInfo(ProfileAuthenticationOption authenticationOption, string usernameToMatch, object userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
{

ProfileInfoCollection profiles = new ProfileInfoCollection();

totalRecords = 0;

// Count profiles only.
if (pageSize == 0)
return profiles;

int counter = 0;
int startIndex = pageSize * (pageIndex - 1);
int endIndex = startIndex + pageSize - 1;

DateTime dt = new DateTime(1900, 1, 1);
if (userInactiveSinceDate != null)
dt = (DateTime)userInactiveSinceDate;

foreach (CustomProfileInfo profile in dal.GetProfileInfo((int)authenticationOption, usernameToMatch, dt, applicationName, out totalRecords))
{
if (counter >= startIndex)
{
ProfileInfo p = new ProfileInfo(profile.UserName, profile.IsAnonymous, profile.LastActivityDate, profile.LastUpdatedDate, 0);
profiles.Add(p);
}

if (counter >= endIndex)
{
break;
}

counter++;
}

return profiles;
}
/// <summary>
///
/// </summary>
/// <param name="authenticationOption"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
/// <returns></returns>
public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
{
CheckParameters(pageIndex, pageSize);

return GetProfileInfo(authenticationOption, null, null, pageIndex, pageSize, out totalRecords);
}
public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
{

CheckParameters(pageIndex, pageSize);

return GetProfileInfo(authenticationOption, usernameToMatch, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
}

public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
{
CheckParameters(pageIndex, pageSize);

return GetProfileInfo(authenticationOption, null, null, pageIndex, pageSize, out totalRecords);
}

public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
{

int inactiveProfiles = 0;

ProfileInfoCollection profiles = GetProfileInfo(authenticationOption, null, userInactiveSinceDate, 0, 0, out inactiveProfiles);

return inactiveProfiles;
}

#endregion

}
回复
bhtfg538 2008-08-30
救命帮忙啊
回复
bhtfg538 2008-08-30
没有人会吗?
回复
bhtfg538 2008-08-30
是 没有 Profile 属性
回复
发动态
发帖子
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
社区公告
暂无公告