none
Adding Forms Based user as an enterprise user in FBA database RRS feed

  • Question

  • Hi,

    I am trying to add an Forms Based user as an enterprise user in Microsoft Project Server 2013. While using the below code for the same, an exception called AdminNTAccountNotFound is encountered. When I create a FBA user from the front end, and add it as an enterprise user, a Random Key is generated in the database. But when I want to do the same operation through code it gives the above exception and no key is generated for that user in the Database. Also, I could not find any property in the Resource.asmx web service to set the same random key. Please provide some pointers on how to set that random key. 
    static protected Cookie authCookie;
    static protected CookieContainer cc;
    
    private static void AuthenticateFBAClaims(string userName, string password)
            {
    
                #region PSIAuthentication
    
                FBA.Authentication spAuthentication = new FBA.Authentication();
                spAuthentication.Url = pwaPath + "/_vti_bin/Authentication.asmx";
                spAuthentication.CookieContainer = new CookieContainer();
                ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
                FBA.LoginResult loginResult = spAuthentication.Login(userName, password);
                authCookie = new Cookie();
    
                if (loginResult.ErrorCode == FBA.LoginErrorCode.NoError)
                {
                    CookieCollection cookies = spAuthentication.CookieContainer.GetCookies(new Uri(spAuthentication.Url));
                    authCookie = cookies[loginResult.CookieName];
                    cc = new CookieContainer();
                    cc.Add(authCookie);
                    Console.WriteLine("Login successful using PSI");
                  
    
                }
                #endregion
    
             
            }
    
     public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                return true;
            }
    
    public static void addUserAuthorization(Guid id, string username)
            {
                Resources.Resource resObj = null;
                Resources.ResourceAuthorizationDataSet resourceAuthDs = null;
                if (AuthenticateFBAClaims("userName", "password"))
                {
                    try
                    {
                        resObj = new Resources.Resource();
             resObj.UnsafeAuthenticatedConnectionSharing = true;
    
                     resObj.CookieContainer = cc;
                        
                     resObj.Url = "ServerName/_vti_bin/PSI/resource.asmx";
    
                        resourceAuthDs = new Resources.ResourceAuthorizationDataSet();
    
    Resources.ResourceAuthorizationDataSet.ResourcesRow resourceAuthRow = resourceAuthDs.Resources.NewResourcesRow();
                        resourceAuthRow.WRES_AD_GUID = Guid.NewGuid();
    
                        resourceAuthRow.RES_UID = id;
    
                        // Set Windows authentication.
                        resourceAuthRow.RES_IS_WINDOWS_USER = false;
    
    
                        SPClaimProviderManager mgr = SPClaimProviderManager.Local;
    
                        if (mgr != null)
                        {
                            SPClaim claim = new SPClaim(SPClaimTypes.UserLogonName, "DemoAD", "http://www.w3.org/2001/XMLSchema#string", SPOriginalIssuers.Format(SPOriginalIssuerType.Forms, "sql-membership"));
                            username = mgr.EncodeClaim(claim);
                        }
    
                        resourceAuthRow.WRES_ACCOUNT = username;
    
                        resourceAuthRow.RES_PREVENT_ADSYNC = true;
                        
                        resourceAuthDs.Resources.AddResourcesRow(resourceAuthRow);
                        
                       resObj.SetResourceAuthorization(resourceAuthDs);
                        
                        addResourceToGroup(id);
                    }
                    catch (Exception ex)
                    {
    
                    }
                    finally
                    {
                        if (resObj != null)
                        {
                            resObj.Dispose();
                        }
                        if (resourceAuthDs != null)
                        {
                            resourceAuthDs.Dispose();
                        }
                    }
                }
            }


    Monday, January 13, 2014 11:10 AM