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

时间:2012-07-05 11:08:33

标签: 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";                
            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
        }          
    }

但是当在WCF服务(托管为Windows服务)中公开相同的方法时,并且当客户端调用此方法时,它无法添加用户。它抛出以下异常:

用户不存在或不是唯一的。

SharePoint日志是:

从成员资格提供程序AspNetSqlMembershipProvider解析http://schemas.microsoft.com/sharepoint/2009/08/claims/userlogonname:forms_user时出错:System.Web.HttpException:无法连接到SQL Server数据库。 ---> System.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)
 在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)
 在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
 在System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo,SqlInternalConnectionTds connHandler,Boolean ignoreSniOpenTimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity,SqlConnection owningObject)
 在System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,Boolean ignoreSniOpenTimeout,Int64 timerExpire,SqlConnection owningObject)
 at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host,String newPassword,Boolean redirectedUserInstance,SqlConnection owningObject,SqlConnectionString connectionOptions,Int64 timerStart)
 在System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject,SqlConnectionString connectionOptions,String newPassword,Boolean redirectedUserInstance)
 在System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity标识,SqlConnectionString connectionOptions,Object providerInfo,String newPassword,SqlConnection owningObject,Boolean redirectedUserInstance)
 在System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,对象poolGroupProviderInfo,DbConnectionPool池,DbConnection owningConnection)
 在System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection,DbConnectionPoolGroup poolGroup)
 在System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
 在System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory)
 在System.Data.SqlClient.SqlConnection.Open()
 at System.Web.Management.SqlServices.GetSqlConnection(String server,String user,String password,Boolean trusted,String connectionString) -   - 内部异常堆栈跟踪结束---
 at System.Web.Management.SqlServices.GetSqlConnection(String server,String user,String password,Boolean trusted,String connectionString)
 at System.Web.Management.SqlServices.SetupApplicationServices(String server,String user,String password,Boolean trusted,String connectionString,String database,String dbFileName,SqlFeatures features,Boolean install)
 在System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName,String dataDir,String connectionString)
 在System.Web.DataAccess.SqlConnectionHelper.EnsureSqlExpressDBFile(String connectionString)
 在System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString,Boolean revertImpersonation)
 在System.Web.Security.SqlMembershipProvider.GetUser(String username,Boolean userIsOnline)
 在Microsoft.SharePoint.Utilities.SPMembershipProviderPrincipalResolver.ResolvePrincipal(字符串输入,布尔值inputIsEmailOnly,SPPrincipalType范围,SPPrincipalSource源,SPUserCollection usersContainer)
 在Microsoft.SharePoint.Administration.Claims.SPFormsClaimProvider.Resolve(SPPrincipalResolver resolver,Boolean inputIsEmailOnly,SPPrincipalSource pricipalSource,SPPrincipalType pricipalType,String resolveInput,List 1 resolved)
at Microsoft.SharePoint.Administration.Claims.SPFormsClaimProvider.FillResolve(Uri context, Boolean allZones, String[] entityTypes, SPClaim resolveInput, List
1已解决)

请帮我解决一下?

由于 普拉卡什

0 个答案:

没有答案