gpt4 book ai didi

C#MVC : User Password Reset Controller: Issues with email addresses as usernames

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:21 35 4
gpt4 key购买 nike

我在 C# MVC 应用程序中编写了以下用于重置用户密码的代码(我使用的是 aspnet 成员(member) api),并在示例教程应用程序(MVC 音乐商店)上成功测试。如果您想先阅读问题描述,请跳到最后。

InactiveUsers View (部分 View )

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Web.Security.MembershipUserCollection>" %>

<table class="normal" style="width: 100%; background-color: White;">
<tr>
<th>User Name</th>
<th>Last Activity date</th>
<th>Locked Out</th>
</tr>
<%foreach (MembershipUser user in Model){ %>


<tr>
<td><%: Html.RouteLink(user.UserName, "AdminPassword", new { username = user.UserName }) %></td>
<td><%: user.LastActivityDate %></td>
<td><%: user.IsLockedOut %></td>
</tr>


<% }%>
</table>

InactiveUsers Controller

public ActionResult InactiveUsers()
{
var users = Membership.GetAllUsers();

return View(users);

}

changeUserPassword GET 和 POST Controller

 public ActionResult changeUserPassword(string username)
{
ViewData["username"] = username;

return View();
}


[HttpPost]
public ActionResult changeUserPassword(ChangePasswordModel model, FormCollection values)
{
string username = values["username"];
string password = values["password"];
string confirmPassword = values["confirmPassword"];

MembershipUser mu = Membership.GetUser(username);

if (password == confirmPassword)
{
if (mu.ChangePassword(mu.ResetPassword(), password))
{
return RedirectToAction("Index", "ControlPanel");
}
else
{
ModelState.AddModelError("", "The current password does not meet requirements");
}
}

return View();
}

我还修改了 Global.asax.cs 文件以适应我在 InactiveUsers 部分中的路由:

        // Added in 10/01/11

RouteTable.Routes.MapRoute(
"AdminPassword", // routename
"ControlPanel/changeUserPassword/{username}",
new { controller = "ControlPanel", action = "changeUserPassword", username = UrlParameter.Optional }
);

// END

现在,当我在 MVC 音乐商店上进行测试时,我所有的用户名都只是单词,例如管理员、用户等。但是,现在我将这段代码应用到我工作场所的一个情况中,但它并没有按计划运行。我工作场所使用的用户名实际上是电子邮件地址,我认为这就是导致问题的原因。

当我在部分 InactiveUsers View 中单击 RouteLink 时,它应该将我带到重置密码页面,其 url 如下所示:

http://localhost:83/ControlPanel/changeUserPassword/example1@gmail.com , 然而,

当我点击 RouteLink 时发生了一个错误,提示无法找到 View changeUserPassword,URL 如下所示:

http://localhost:83/ControlPanel/changeUserPassword/example1%40gmail.com - 看看“@”符号是如何弄乱的?

我还调试了代码,在我的 GET changeUserPassword 中,用户名填充正确:example1@gmail.com,所以我认为只是 URL 搞砸了?

如果我手动输入 URL,会显示 changeUserPassword View ,但密码重置功能不起作用。在 if (mu.ChangePassword(mu.ResetPassword(), password)) 行抛出“未设置对象实例的对象引用”异常。

我想如果我能解决第一个问题(URL '@' 符号问题),它可能会帮助我解决第二个问题。

任何帮助将不胜感激:)

堆栈跟踪 - 根据要求

来源错误:

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

[InvalidOperationException: The view 'changeUserPassword' or its master was not found. The following locations were searched:
~/Views/ControlPanel/changeUserPassword.aspx
~/Views/ControlPanel/changeUserPassword.ascx
~/Views/Shared/changeUserPassword.aspx
~/Views/Shared/changeUserPassword.ascx]
System.Web.Mvc.ViewResult.FindView(ControllerContext context) +495
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.<>c__DisplayClass14.<InvokeActionResultWithFilters>b__11() +60
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +391
System.Web.Mvc.<>c__DisplayClass16.<InvokeActionResultWithFilters>b__13() +61
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +285
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830
System.Web.Mvc.Controller.ExecuteCore() +136
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +111
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +65
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +42
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +141
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +52
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

最佳答案

你知道 %40 是 @ 符号的 URL 编码版本吗?您的 URL 中的数据没有弄乱,它是 URL 编码的。这是必需的,因为您的数据包含一个在 URL 中有意义的符号,因此必须以不破坏 URL 的方式对其进行编码。

您可以在这里尝试 URL 编码/解码: http://meyerweb.com/eric/tools/dencoder/

关于C#MVC : User Password Reset Controller: Issues with email addresses as usernames,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4645635/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com