How To: Create user profile in SSP from FBA site

--------------------------------------------------------------------------------------
Web Service approach
--------------------------------------------------------------------------------------

You can use UserProfileService.asmx webservice to achieve the solution. There are couple of methods in this webservice to deal with user profiles like create User Profile, modify User Profile, delete User Profile etc...
Please follow the following steps to test the sample code.

1.    You don’t have to extend the SSP in FBA mode. Set a windows domain user as an administrator in the SSP and give that user the permission under “Personalization services permissions” in SSP.
2.    Please put the following code in the webpart and deploy it on the FBA site.(please modify the webservice URL, domain user)
3.    You can use UserProfileService.asmx webservice to set the web reference in your webpart.

private void GetCurrentUserAccount()
{
            System.Security.Principal.WindowsIdentity currUser = (System.Security.Principal.WindowsIdentity)CreateIdentity("test1", "terminator", "Terminator1");
            System.Security.Principal.WindowsImpersonationContext impContext = currUser.Impersonate();

            SSP30060.UserProfileService ssp = new SSP30060.UserProfileService();
            ssp.Url = @"<https://terminator:30060/ssp/admin/_vti_bin/UserProfileService.asmx>";

            ssp.Credentials = System.Net.CredentialCache.DefaultCredentials;

            ssp.CreateUserProfileByAccountName("terminator\\test1");        
            impContext.Undo();
            impContext = null;
            currUser = null;
}
protected static WindowsIdentity CreateIdentity(string User, string Domain, string Password)
{
            // The Windows NT user token.
            IntPtr tokenHandle = new IntPtr(0);

            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_NETWORK = 3;

            tokenHandle = IntPtr.Zero;

            // Call LogonUser to obtain a handle to an access token.
            bool returnValue = LogonUser(User, Domain, Password,
            LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
            ref tokenHandle);
            if (false == returnValue)
            {
                int ret = Marshal.GetLastWin32Error();
                throw new Exception("LogonUser failed with error code: " + ret);
            }

            System.Diagnostics.Debug.WriteLine("Created user token: " + tokenHandle);
            //The WindowsIdentity class makes a new copy of the token.
            //It also handles calling CloseHandle for the copy.
            WindowsIdentity id = new WindowsIdentity(tokenHandle);
            CloseHandle(tokenHandle);
            return id;
}

[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);

--------------------------------------------------------------------------------------
Extending SSP approach
--------------------------------------------------------------------------------------
1.    Extend the SharePoint service provider(SSP) in FBA mode.
2.    Open the SSP in windows authentication mode with window admin user account .
3.    Go to “Personalization services permissions” under “User Profiles and My Sites”.
4.    Add the aspdb user with “Manage User Profiles” permission.
5.    Try to run the provided code as a web part in button_click event .
6.    Click the button and verify whether it creates a profile in SSP -> “User Profiles”

            try
            {
                SPSecurity.RunWithElevatedPrivileges(
                    delegate()
                    {
                        using (SPSite site = new SPSite("<https://terminator:30001/sites/FBA/>"))
                        {
                            SPWeb web = site.OpenWeb();
                            site.AllowUnsafeUpdates = true;
                            web.AllowUnsafeUpdates = true;
                            web.Update();

                            ServerContext context = ServerContext.GetContext("SSP-30010");
                            UserProfileManager profileManager = new UserProfileManager(context);
                            UserProfile profile = profileManager.CreateUserProfile("AspNetSqlMembershipProvider:" + UserName);
                            if (null == profile)
                            {
                                throw new Exception("Failed to Create User with account :");
                            }
                            else
                            {
                                profile["PreferredName"].Value = (string)UserName;
                                profile.Commit();
                            }
                        }
                    }
                    );
            }
            catch (Exception ex)
            {
                return " >error : " + ex.Message.ToString();
            }
            return "Success";

 

Keywords: FBA site, form based authenticated site,  form based authentication site, SSP user profiles. shared service provider user profile, creating an user profile in a shared service provider.