尝试删除当前成员时出错

时间:2013-02-15 17:19:46

标签: asp.net umbraco profile-provider

我有一个在Umbraco网站上运行的.NET用户控件,我希望能够从代码隐藏中删除当前成员(我意识到这听起来不是一个好的计划,但这确实适用于正常的ASP.NET Forms网站),但调用System.Web.Security.Membership.DeleteUser(usernameToDelete, trueOrFalse);给了我(在我重定向到的页面上):

No member with username 'test10@test.test' exists
Exception Details: System.Configuration.Provider.ProviderException: No member with username 'test10@test.test' exists
[ProviderException: No member with username 'test10@test.test' exists]
   umbraco.providers.members.UmbracoProfileProvider.SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) +346
   System.Configuration.SettingsBase.SaveCore() +402
   System.Configuration.SettingsBase.Save() +109
   System.Web.Profile.ProfileBase.SaveWithAssert() +31
   System.Web.Profile.ProfileBase.Save() +72
   System.Web.Profile.ProfileModule.OnLeave(Object source, EventArgs eventArgs) +9025062
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

为什么在删除用户和更改页面后尝试设置属性值?我看不出如何设置/删除当前的Umbraco成员。


编辑:我正在使用umbraco v 4.11.1(汇编版本:1.0.4715.27659)

这是我尝试过的最长版本的注销代码仍然会出错:

// sort out problems with umbraco caches:
Member.RemoveMemberFromCache(Member.CurrentMemberId());
Member.ClearMemberFromClient(Member.CurrentMemberId());

Roles.RemoveUserFromRole(Page.User.Identity.Name, JobShopRoles.RoleNames.Candidate.ToString());

//logout from: http://stackoverflow.com/questions/412300/formsauthentication-signout-does-not-log-the-user-out
FormsAuthentication.SignOut();
Page.Session.Clear();  // This may not be needed -- but can't hurt
Page.Session.Abandon();

// Clear authentication cookie
HttpCookie rFormsCookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
rFormsCookie.Expires = DateTime.Now.AddYears(-1);
Page.Response.Cookies.Add(rFormsCookie);

// Clear session cookie 
HttpCookie rSessionCookie = new HttpCookie("ASP.NET_SessionId", "");
rSessionCookie.Expires = DateTime.Now.AddYears(-1);
Page.Response.Cookies.Add(rSessionCookie);
// Invalidate the Cache on the Client Side

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
Page.Response.Cache.SetNoStore();
//END logout

System.Web.Security.Membership.DeleteUser(currentUser.UserName, true);

//TODO: this will consistently give an error trying to update a property on a deleted member. Qs:
//http://stackoverflow.com/questions/14899945/error-trying-to-delete-current-member
//http://our.umbraco.org/forum/core/general/38455-Error-trying-to-delete-current-Member
Response.Redirect("/", false);

1 个答案:

答案 0 :(得分:0)

如果您在删除之前从代码隐藏中注销当前用户会怎样?

看起来已删除的用户仍然登录并存在于当前会话中,因此Umbraco profileprovider会尝试对其执行某些操作...

编辑: 在FormsAuthentication Logout之后尝试这两种方法,看看它是否有帮助:

Member.RemoveMemberFromCache(Member.CurrentMemberId());
Member.ClearMemberFromClient(Member.CurrentMemberId());

EDIT2:

我查看了UmbracoProfileProvider类以查看导致错误的原因:

   public override void SetPropertyValues(SettingsContext context,
 SettingsPropertyValueCollection collection) {

                string username = (string)context["UserName"];
                bool authenticated = (bool)context["IsAuthenticated"];

                if (String.IsNullOrEmpty(username) || collection.Count == 0)
                    return;

                Member m = Member.GetMemberFromLoginName(username);
                if (m == null)
                    throw new ProviderException(String.Format("No member with username '{0}' exists", username));


                foreach (SettingsPropertyValue spv in collection) {
                    if (!authenticated && !(bool)spv.Property.Attributes["AllowAnonymous"])
                        continue;

                    if (m.getProperty(spv.Name) != null)
                        m.getProperty(spv.Name).Value = spv.PropertyValue;
                }

            }

在提供程序尝试根据用户名检索当前成员后,您可以看到异常消息。 无论哪种方式,您的上下文仍然包含UserName或SettingsPropertyValueCollection计数是> 0因此,注销成员的代码似乎仍然留下了一些东西。

尝试使用此部件而不是您拥有的所有注销码:

   Member.RemoveMemberFromCache(Member.CurrentMemberId());
   Member.ClearMemberFromClient(Member.CurrentMemberId());
   FormsAuthentication.SignOut();
   Roles.DeleteCookie();
   HttpContext.Current.Session.Clear();
   System.Web.Security.Membership.DeleteUser(usernameToDelete, false);

这将从缓存和客户端中删除并清除该成员,注销,删除Role cookie并清除当前会话。最后,在注销后,使用其用户名删除该成员。