以编程方式将基于表单的用户添加到SharePoint网站

以编程方式将基于表单的用户添加到SharePoint网站,sharepoint,sharepoint-2010,sharepoint-2007,c#-3.0,forms-authentication,Sharepoint,Sharepoint 2010,Sharepoint 2007,C# 3.0,Forms Authentication,我在SharePoint 2010中有一个web应用程序,其身份验证模式为Windows。我扩展了web应用区域:使用基于表单的身份验证的Intranet。 我已经创建了一个示例C控制台应用程序,用于将基于表单的用户添加到站点,它成功地添加了用户 方法: private void AddFormsBasedUserToSite(){ try { string url = "https://appserver:36732";

我在SharePoint 2010中有一个web应用程序,其身份验证模式为Windows。我扩展了web应用区域:使用基于表单的身份验证的Intranet。 我已经创建了一个示例C控制台应用程序,用于将基于表单的用户添加到站点,它成功地添加了用户

方法:

    private void AddFormsBasedUserToSite(){
        try
        {
            string url = "https://appserver:36732";                
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        string userName = "i:0#.f|aspnetsqlmembershipprovider|forms_user";
                        web.SiteUsers.Add(userName, null, null, null);
                        web.Update();                            
                    }

                }
            });                
        }
        catch (Exception e)
        {
            // Log the exception
        }          
    }
但是,当相同的方法在作为windows服务托管的WCF服务中公开时,并且当客户端调用此方法时,它无法添加用户。它引发以下异常:

用户不存在或不唯一

SharePoint日志为:

解析错误:来自成员资格提供程序AspNetSqlMembershipProvider的forms\u用户:System.Web.HttpException:无法连接到SQL Server数据库。-->System.Data.SqlClient.SqlException:建立到SQL Server的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。提供程序:SQL网络接口,错误:26-定位指定的服务器/实例时出错 位于System.Data.SqlClient.SqlInternalConnection.OnErrorSqlException异常,布尔断开连接 在System.Data.SqlClient.TdsParser.throweexception和warningdsparserstateobj处 位于System.Data.SqlClient.TdsParser.ConnectServerInfo serverInfo、sqlinternalconnectionds connHandler、Boolean ignoresnopentimeout、Int64 timerExpire、Boolean encrypt、Boolean trustServerCert、Boolean integratedSecurity、SqlConnection owningObject 在System.Data.SqlClient.SqlInternalConnectionDS.AttemptOneLoginServerInfo serverInfo、String newPassword、Boolean IgnoresInOpenTimeout、Int64 TimerSpire、SqlConnection owningObject中 在System.Data.SqlClient.SqlInternalConnectionDS.LoginOfAlioverString主机上,输入字符串newPassword,布尔值redirectedUserInstance,SqlConnectionOwningObject,SqlConnectionString connectionOptions,Int64 timerStart 在System.Data.SqlClient.SqlInternalConnectionDS.OpenLoginenListSqlConnectionOwningObject、SqlConnectionString connectionOptions、String newPassword、Boolean redirectedUserInstance 位于System.Data.SqlClient.SqlInternalConnectionDS..ctordConnectionPoolIdentity、SqlConnectionString connectionOptions、Object providerInfo、String newPassword、SqlConnection owningObject、Boolean redirectedUserInstance 位于System.Data.SqlClient.SqlConnectionFactory.CreateConnectionDbConnectionOptions选项、对象池组ProviderInfo、数据库连接池池、数据库连接所有者连接 在System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnectionDBConnectionOwningConnection中,DbConnectionPoolGroup poolGroup 位于System.Data.ProviderBase.DbConnectionFactory.GetConnectionDbConnection owningConnection 在System.Data.ProviderBase.DbConnectionClosed.OpenConnectionDbConnection outerConnection、DbConnectionFactory connectionFactory中 在System.Data.SqlClient.SqlConnection.Open处 在System.Web.Management.SqlServices.GetSqlConnectionString服务器上,字符串用户,字符串密码,布尔值可信,字符串connectionString- -内部异常堆栈跟踪的结束-- 在System.Web.Management.SqlServices.GetSqlConnectionString服务器上,字符串用户,字符串密码,布尔值可信,字符串connectionString 在System.Web.Management.SqlServices.setupApplicationServiceString服务器上,字符串用户,字符串密码,布尔可信,字符串连接字符串,字符串数据库,字符串dbFileName,SqlFeatures功能,布尔安装 在System.Web.DataAccess.SqlConnectionHelper.CreateMdfFileString fullFileName、String dataDir、String connectionString中 位于System.Web.DataAccess.SqlConnectionHelper.EnsureSqlExpressDBFileString connectionString 在System.Web.DataAccess.SqlConnectionHelper.GetConnectionString connectionString中,布尔值还原模拟 位于System.Web.Security.SqlMembershipProvider.GetUserString用户名处,布尔userIsOnline 在Microsoft.SharePoint.Utilities.SPMembershipProviderPrincipalResolver.ResolvePrincipalString输入、Boolean输入实用程序、SPPrincipalType作用域、SPPrincipalSource源、SPUsersContainer 在Microsoft.SharePoint.Administration.Claims.SPFormsClaimProvider.ResolvesPrincipalResolver解析程序中,布尔输入仅限,SPPrincipalSource PrincipalSource,SPPrincipalType PrincipalType,字符串resolveInput,列表1已解析 在Microsoft.SharePoint.Administration.Claims.SPFormsClaimProvider.FillResolveUri上下文中,布尔allZones,字符串[]entityTypes,SPClaim resolveInput,列表1已解析

请帮我弄清楚

谢谢
Prakash

每次调用该方法时,您可能应该考虑执行,而不是添加。也就是说,您显示的错误看起来更像是配置错误。确保STS中的web.config具有适当的连接字符串。谢谢Torres。我已经重新检查了我的web.config,但代码在web服务中仍然不起作用。我想知道为什么相同的代码可以工作
s在控制台应用程序中,在web服务中不起作用。另一种可能的想法是:您的服务正在运行的用户可能没有权限打开套接字/连接到您的SQL server?您是否在SharePoint场上的服务器上运行代码?您无法从远程服务器访问该数据库。当您托管WCF服务时,AppPool用户不能是网络服务或任何本地系统帐户,必须是在SharePoint中授权的用户。为这些服务使用单独的应用程序池,并明确设置应用程序池用户。每次调用该方法时,您可能应该考虑执行而不是添加。也就是说,您显示的错误看起来更像是配置错误。确保STS中的web.config具有适当的连接字符串。谢谢Torres。我已经重新检查了我的web.config,但代码在web服务中仍然不起作用。我想知道为什么相同的代码在console应用程序中有效,而在web服务中无效。另一种可能的想法是:您的服务所运行的用户可能没有打开套接字/连接到SQL server的权限?您是否在SharePoint场上的服务器上运行代码?您无法从远程服务器访问该数据库。当您托管WCF服务时,AppPool用户不能是网络服务或任何本地系统帐户,必须是在SharePoint中授权的用户。为这些服务使用单独的应用程序池,并显式设置应用程序池用户