Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
DZone MVB

Links

Social

SharePoint: creating SharePoint accounts for Live ID users

Couple of weeks ago I started adding Windows Live ID authentication support to SharePoint. I used Community Kit for SharePoint and I also made here some notes about it. There was one problem – although user is authenticated there is no SharePoint user and it is not possible assign it to any roles. Here is some advices how to create SharePoint user account when new user is registered after logging in using Live ID.

  1. Open Windows Live ID Authentication project with Visual Studio.
  2. Open file Code/liveinfo.cs.
  3. Move to Submit_Click() method and find the following line:

    bup.Update();


  4. Add the following code after line mentioned above. This code runs method AddNewSPUser then creates user account also to SharePoint. This way you achieve link between LiveID authenticated user account and SharePoint user account.

    var sec = new SPSecurity.CodeToRunElevated(

        delegate()

        {

            AddNewSPUser(User.Identity.Name, tbDisplayName.Text);

        });

    SPSecurity.RunWithElevatedPrivileges(sec);


  5. Add the following method to the end of the class:

    private static void AddNewSPUser(string userName, string name)

    {

        using (SPSite site = new SPSite(SPContext.Current.Web.Url))

        {

            using (SPWeb web = site.RootWeb)

            {

                site.AllowUnsafeUpdates = true;

                web.AllowUnsafeUpdates = true;

     

                SPUser user = web.EnsureUser(userName);

                user.Name = name;

                user.Update();

                web.SiteGroups["MySite visitor"].AddUser(user);

                web.Update();

                web.AllowUnsafeUpdates = false;

            }

            site.AllowUnsafeUpdates = false;

        }

    }


  6. Compile the code and deploy it to server. Also don’t forget to recycle application bool of web application to where you deployed the solution.

Now, after new user joins your site, the new user account is also created to SharePoint. By default the new user account is assigned to “MySite visitor” group. Make sure you have this group created or change the name of default group before compiling the code.

Posted: Feb 27 2009, 06:23 PM by DigiMortal | with 4 comment(s)
Filed under: ,

Comments

Zelgo said:

To use Live ID Users in SharePoint you can also find a complete solution with souce code in following url

www.shetabtech.com/.../default.aspx

# March 4, 2009 2:31 AM

Josh said:

This works, but the user doesn't show up in "People And Groups: All People".  Do you know why?

# March 6, 2009 6:12 PM

DigiMortal said:

Does it show up in the group you put the user by default?

# March 7, 2009 9:53 AM

madnik7 said:

Here is another solution that support OpenID and SharePoint 2010 too

www.shetabtech.com/.../default.aspx

# September 24, 2010 4:30 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)