Asp.net 尝试删除当前成员时出错
我有一个运行在Umbraco网站上的.NET usercontrol,我希望能够从codebehind中删除当前成员(我意识到这听起来不是一个很好的计划,但在普通的ASP.NET表单网站上确实有效),但是调用Asp.net 尝试删除当前成员时出错,asp.net,umbraco,profile-provider,Asp.net,Umbraco,Profile Provider,我有一个运行在Umbraco网站上的.NET usercontrol,我希望能够从codebehind中删除当前成员(我意识到这听起来不是一个很好的计划,但在普通的ASP.NET表单网站上确实有效),但是调用System.Web.Security.Membership.DeleteUser(usernameToDelete,trueOrFalse)给我(在我重定向到的页面上): 为什么在我删除了用户并更改了页面之后,它还要尝试设置属性值?我看不到如何设置/删除当前的Umbraco成员 编辑:我
System.Web.Security.Membership.DeleteUser(usernameToDelete,trueOrFalse)代码>给我(在我重定向到的页面上):
为什么在我删除了用户并更改了页面之后,它还要尝试设置属性值?我看不到如何设置/删除当前的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);
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;
}
}
如果在删除当前用户之前从codebehind注销该用户,会发生什么情况
看起来被删除的用户仍在登录并存在于当前会话中,因此Umbraco profileprovider尝试对其进行处理
编辑:
在FormsAuthentication注销后尝试以下两种方法,看看是否有帮助:
Member.RemoveMemberFromCache(Member.CurrentMemberId());
Member.ClearMemberFromClient(Member.CurrentMemberId());
编辑2:
我查看了UmbracoProfileProvider类,以查看导致错误的原因:
// 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);
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;
}
}
在提供程序尝试基于用户名检索当前成员后,您可以看到异常消息。
无论哪种方式,您的上下文仍然包含用户名,或者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);
这将从缓存和客户端中删除和清除成员、注销、删除角色cookie并清除当前会话。最后,在注销后,使用用户名删除该成员。同样的事情。我很难让FormsAuthentication.SignOut()正常工作,但在这个优秀网站的帮助下,我删除了一些cookies,使其正常工作。我认为,一旦该成员注销,我就可以很好地删除他们,但我将放弃该会话,因此我不能使用该会话来允许往返,因此这意味着将他们添加到“删除队列”或类似队列中:/请参阅上面的编辑,这可能会有所帮助。如果没有,添加处理注销的代码部分。我将发布我所拥有的代码的最长版本,但仍然无法运行…那么Roles.DeleteCookie()应该修复它吗?因为我已经有其他的台词了。我会尽快试一试的!只是尝试了同样的问题。我将把这个版本添加到问题中。顺便问一下,您使用的是哪个Umbraco版本?