在(通过SimpleMembership)我的ASP.NET MVC应用程序使用表单验证,我怎么删除用户/帐户?
该WebSecurity类不公开DeleteUser。 在云雀,我想:
WebSecurity.InitializeDatabaseConnection(
"MyDbConnection", "Users", "Id", "UserName", autoCreateTables: true);
new SimpleMembershipProvider().DeleteUser(userName, true);
但抱怨我没有初始化SimpleMembership提供商。 在任何情况下,我将非常感谢一些示例代码,说明如何删除用户。 谢谢!
短发
What happens if you just do Membership.DeleteUser(username,true)
. You might get a little prompt for adding a using directive on Membership
. If you have it configured properly, you shouldn't need to be creating new SimpleMembershipProvider instance.
If you create it on the fly like that, you'll need to set connections on that object and configure it programmatically(it has no clue about the connection you created above). Usually people do that in web.config, but if you created the app using the forms authentication template, then you should have that taken care of automatically.
Your provider my have this bug for which is discussed and solved here: Membership.DeleteUser is not deleting all related rows of the user
PussInBoots是绝对正确的,虽然这总是抛出一个违反外键约束,我如果被删除的用户已添加到任何角色。 我敢肯定,这是由PussInBoots'推断‘// TODO:在此添加删除逻辑’的评论,但我通常会清理的角色成员第一次是这样的:
[HttpPost]
public ActionResult Delete(string userName, FormCollection collection)
{
try
{
// TODO: Add delete logic here
if (Roles.GetRolesForUser(userName).Count() > 0)
{
Roles.RemoveUserFromRoles(userName, Roles.GetRolesForUser(userName));
}
((SimpleMembershipProvider)Membership.Provider).DeleteAccount(userName); // deletes record from webpages_Membership table
((SimpleMembershipProvider)Membership.Provider).DeleteUser(userName, true); // deletes record from UserProfile table
return RedirectToAction("Index");
}
catch
{
return View(userName);
}
}
你可能需要的东西是这样的:
//
// GET: /Members/Delete?userName=someuser
public ActionResult Delete(string userName)
{
var user = context.UserProfiles.SingleOrDefault(u => u.UserName == userName);
return View(user);
}
//
// POST: /Members/Delete?userName=someuser
[HttpPost]
public ActionResult Delete(string userName, FormCollection collection)
{
try
{
// TODO: Add delete logic here
((SimpleMembershipProvider)Membership.Provider).DeleteAccount(userName); // deletes record from webpages_Membership table
((SimpleMembershipProvider)Membership.Provider).DeleteUser(userName, true); // deletes record from UserProfile table
return RedirectToAction("Index");
}
catch
{
return View(userName);
}
}
我是从Membership.DeleteUser运行我的单元测试时得到的异常System.NotSupportedException。 问题是在app.config有“DefaultProvider”设置为“ClientAuthenticationMembershipProvider”,正如你可以看到这里是“不使用这个类”。
伦敦定盘来更新我的app.config符合我的web.config并正确配置的默认提供:
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="Crelate.Properties.Settings.DatabaseMembershipServicesConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
嘿只是想发布此运行到ObjectContext的状态问题下面PussInBoots例如后任何人,因为我有同样的问题...
如果您正在访问额外的用户数据,您将需要使用的数据上下文删除该用户:
context.Users.Remove(user);
而不是:
((SimpleMembershipProvider)Membership.Provider).DeleteUser(userName, true);
这将让你EF方面最新的并从数据库用户。