如题,在建立网站里,自定义的profileprovider可以成功执行save(),可是转到web应用程序上,就不执行了(即编译通过,但是不会自动跳转到自定义的SetPropertyValues函数里)。
先贴我的webconfig
<?xml version="1.0" encoding="utf-8"?><!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<!--<?xml version="1.0" encoding="UTF-8"?>
注意: 除了手动编辑此文件外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
有关设置和注释的完整列表可以在
machine.config.comments 中找到,该文件通常位于
\Windows\Microsoft.Net\Framework\vx.x\Config 中
-->
<configuration>
<appSettings>
<add key="ConnectionString" value="server=(local);Uid=sa;pwd=tc13376543186;database=DBFlower;" />
</appSettings>
<connectionStrings>
<remove name="LocalSqlServers" />
<add connectionString="Data Source=JY-PC;Initial Catalog=aspnetdb;User ID=sa;Password=tc13376543186" name="LocalSqlServers" />
<add connectionString="Data Source=JY-PC;Initial Catalog=MyProfile;User ID=sa;Password=tc13376543186" name="ProfileService" />
<add connectionString="Data Source=JY-PC;Initial Catalog=XingProfile;User ID=sa;Password=tc13376543186" name="ProfileServiced" /> <!--<add name="memberConnectionString" connectionString="Data Source=(local);Integrated Security=SSPI;Initial Catalog=aspnetdb;User ID=sa;"/>-->
</connectionStrings>
<system.web>
<roleManager enabled="true" defaultProvider="CustomSqlprovider" cacheRolesInCookie="true" cookieName=".MyRolesCookie" cookieTimeout="30" cookieSlidingExpiration="false" cookieProtection="All">
<providers>
<add name="CustomSqlprovider" type="System.Web.Security.SqlRoleProvider" connectionStringName="LocalSqlServers" applicationName="k" />
</providers>
</roleManager>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
<authentication mode="Forms">
<forms name="AuthCookie" loginUrl="login.html" />
</authentication>
<membership defaultProvider="MyMembershipProvider">
<providers>
<add connectionStringName="LocalSqlServers" applicationName="as" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" name="MyMembershipProvider" type="System.Web.Security.SqlMembershipProvider" />
<add connectionStringName="LocalSqlServers" applicationName="/" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed" name="MyMembershipProvider2" type="System.Web.Security.SqlMembershipProvider" />
</providers>
</membership>
<profile defaultProvider="FactoredProfileProvider1">
<providers>
<clear />
<!--<add connectionStringName="LocalSqlServers" applicationName="as" name="MySqlProvider" type="System.Web.Profile.SqlProfileProvider" />
<add name="FactoredProfileProvider"
type="FactoredProfileProvider"
connectionStringName="ProfileService"
updateUserProcedure="Users_Update"
getUserProcedure="Users_GetByUserName"/>LocalSqlServers-->
<add name="FactoredProfileProvider1"
type="Web.FactoredProfileProvider"
connectionStringName="ProfileServiced"
updateUserProcedure="Users_Update"
getUserProcedure="Users_GetByUserName"/>
</providers>
<properties>
<!--<add name="AddressName" type="String" provider="FactoredProfileProvider"/>
<add name="AddressCountry" type="String" provider="FactoredProfileProvider"/>-->
<add name="Head" type="System.byte[]" provider="FactoredProfileProvider1"/>
</properties> </profile>
<authorization>
<!--设置资源为受保护,匿名不允许访问-->
<deny users="?" />
</authorization>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
</system.web>
</configuration>
还有我的自定义提供程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace Web
{
public class FactoredProfileProvider : ProfileProvider
{
public FactoredProfileProvider()
{
}
private string name;
public override string Name
{
get
{
return name;
}
}
private string connectionString;
public string ConnectionString
{
get { return connectionString; }
}
private string updateProcedure;
public string UpdateUserProcedure
{
get { return updateProcedure; }
}
private string getProcedure;
public string GetUserProcedure
{
get { return getProcedure; }
}
public override void Initialize(string name, NameValueCollection config)
{
this.name = name;
string ny = config["connectionStringName"].ToString();
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings[config["connectionStringName"]];
if (connectionStringSettings == null || connectionStringSettings.ConnectionString.Trim() == "")
{
throw new HttpException("connection is null");
}
else
{
connectionString = connectionStringSettings.ConnectionString;
}
updateProcedure = config["updateUserProcedure"];
if (updateProcedure.Trim() == "")
{
throw new HttpException("updateProcedure is null");
}
getProcedure = config["getUserProcedure"];
if (getProcedure.Trim() == "")
{
throw new HttpException("getProcedure is null");
}
//base.Initialize(name, config);
}
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection properties)
{
SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(getProcedure, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserName", (string)context["UserName"]));
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
reader.Read();
foreach (SettingsProperty property in properties)
{
SettingsPropertyValue value = new SettingsPropertyValue(property);
if (reader.HasRows)
{
value.PropertyValue = reader[property.Name];
}
values.Add(value);
}
reader.Close();
}
finally
{
con.Close();
}
return values;
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values)
{
//throw new NotImplementedException();
//SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(updateProcedure, con);
cmd.CommandType = CommandType.StoredProcedure;
foreach (SettingsPropertyValue value in values)
{
cmd.Parameters.Add(new SqlParameter(value.Name, value.PropertyValue));
}
cmd.Parameters.Add(new SqlParameter("@UserName", (string)context["UserName"]));
try
{
con.Open();
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
} }
先贴我的webconfig
<?xml version="1.0" encoding="utf-8"?><!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<!--<?xml version="1.0" encoding="UTF-8"?>
注意: 除了手动编辑此文件外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
有关设置和注释的完整列表可以在
machine.config.comments 中找到,该文件通常位于
\Windows\Microsoft.Net\Framework\vx.x\Config 中
-->
<configuration>
<appSettings>
<add key="ConnectionString" value="server=(local);Uid=sa;pwd=tc13376543186;database=DBFlower;" />
</appSettings>
<connectionStrings>
<remove name="LocalSqlServers" />
<add connectionString="Data Source=JY-PC;Initial Catalog=aspnetdb;User ID=sa;Password=tc13376543186" name="LocalSqlServers" />
<add connectionString="Data Source=JY-PC;Initial Catalog=MyProfile;User ID=sa;Password=tc13376543186" name="ProfileService" />
<add connectionString="Data Source=JY-PC;Initial Catalog=XingProfile;User ID=sa;Password=tc13376543186" name="ProfileServiced" /> <!--<add name="memberConnectionString" connectionString="Data Source=(local);Integrated Security=SSPI;Initial Catalog=aspnetdb;User ID=sa;"/>-->
</connectionStrings>
<system.web>
<roleManager enabled="true" defaultProvider="CustomSqlprovider" cacheRolesInCookie="true" cookieName=".MyRolesCookie" cookieTimeout="30" cookieSlidingExpiration="false" cookieProtection="All">
<providers>
<add name="CustomSqlprovider" type="System.Web.Security.SqlRoleProvider" connectionStringName="LocalSqlServers" applicationName="k" />
</providers>
</roleManager>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
<authentication mode="Forms">
<forms name="AuthCookie" loginUrl="login.html" />
</authentication>
<membership defaultProvider="MyMembershipProvider">
<providers>
<add connectionStringName="LocalSqlServers" applicationName="as" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" name="MyMembershipProvider" type="System.Web.Security.SqlMembershipProvider" />
<add connectionStringName="LocalSqlServers" applicationName="/" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed" name="MyMembershipProvider2" type="System.Web.Security.SqlMembershipProvider" />
</providers>
</membership>
<profile defaultProvider="FactoredProfileProvider1">
<providers>
<clear />
<!--<add connectionStringName="LocalSqlServers" applicationName="as" name="MySqlProvider" type="System.Web.Profile.SqlProfileProvider" />
<add name="FactoredProfileProvider"
type="FactoredProfileProvider"
connectionStringName="ProfileService"
updateUserProcedure="Users_Update"
getUserProcedure="Users_GetByUserName"/>LocalSqlServers-->
<add name="FactoredProfileProvider1"
type="Web.FactoredProfileProvider"
connectionStringName="ProfileServiced"
updateUserProcedure="Users_Update"
getUserProcedure="Users_GetByUserName"/>
</providers>
<properties>
<!--<add name="AddressName" type="String" provider="FactoredProfileProvider"/>
<add name="AddressCountry" type="String" provider="FactoredProfileProvider"/>-->
<add name="Head" type="System.byte[]" provider="FactoredProfileProvider1"/>
</properties> </profile>
<authorization>
<!--设置资源为受保护,匿名不允许访问-->
<deny users="?" />
</authorization>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
</system.web>
</configuration>
还有我的自定义提供程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace Web
{
public class FactoredProfileProvider : ProfileProvider
{
public FactoredProfileProvider()
{
}
private string name;
public override string Name
{
get
{
return name;
}
}
private string connectionString;
public string ConnectionString
{
get { return connectionString; }
}
private string updateProcedure;
public string UpdateUserProcedure
{
get { return updateProcedure; }
}
private string getProcedure;
public string GetUserProcedure
{
get { return getProcedure; }
}
public override void Initialize(string name, NameValueCollection config)
{
this.name = name;
string ny = config["connectionStringName"].ToString();
ConnectionStringSettings connectionStringSettings =
ConfigurationManager.ConnectionStrings[config["connectionStringName"]];
if (connectionStringSettings == null || connectionStringSettings.ConnectionString.Trim() == "")
{
throw new HttpException("connection is null");
}
else
{
connectionString = connectionStringSettings.ConnectionString;
}
updateProcedure = config["updateUserProcedure"];
if (updateProcedure.Trim() == "")
{
throw new HttpException("updateProcedure is null");
}
getProcedure = config["getUserProcedure"];
if (getProcedure.Trim() == "")
{
throw new HttpException("getProcedure is null");
}
//base.Initialize(name, config);
}
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection properties)
{
SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(getProcedure, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserName", (string)context["UserName"]));
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
reader.Read();
foreach (SettingsProperty property in properties)
{
SettingsPropertyValue value = new SettingsPropertyValue(property);
if (reader.HasRows)
{
value.PropertyValue = reader[property.Name];
}
values.Add(value);
}
reader.Close();
}
finally
{
con.Close();
}
return values;
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values)
{
//throw new NotImplementedException();
//SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(updateProcedure, con);
cmd.CommandType = CommandType.StoredProcedure;
foreach (SettingsPropertyValue value in values)
{
cmd.Parameters.Add(new SqlParameter(value.Name, value.PropertyValue));
}
cmd.Parameters.Add(new SqlParameter("@UserName", (string)context["UserName"]));
try
{
con.Open();
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
} }
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;namespace Web
{
public class ProfileCommon:ProfileBase
{
public byte[] Head
{
get
{
if (HttpContext.Current.Profile.GetPropertyValue("Head") == DBNull.Value)
{
return null;
}
else
return (byte[])HttpContext.Current.Profile.GetPropertyValue("Head");
}
set
{
HttpContext.Current.Profile.SetPropertyValue("Head", value);
}
} }
}
补充一下,只是我自己写的ProfileCommon,因为web APP不像网站一样能自动派生这个类,有点郁闷...