using (SPWeb userWeb = SPContext.Current.Web)
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite currentSite = new SPSite(userWeb.Site.ID))
                        {
                            using (SPWeb currentWeb = currentSite.AllWebs[userWeb.ID])
                            {
                                //allow unsafe updates
                                bool currentUnsafeSetting = currentWeb.AllowUnsafeUpdates;
                                currentWeb.AllowUnsafeUpdates = true;
                                returnValue = currentWeb.EnsureUser(loginName);
                                currentWeb.Update();
                                currentWeb.AllowUnsafeUpdates = currentUnsafeSetting;
                            }
                        }
                    });
                }
Advertisements