C#MVCアプリケーションでユーザーパスワードをリセットするための以下のコードを記述し(aspnetメンバーシップAPIを使用しています)、サンプルチュートリアルアプリケーション(MVCミュージックストア)で正常にテストしました。最初に問題の説明を読みたい場合は、最後までスキップしてください。
InactiveUsersビュー(部分ビュー)
<%@ 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コントローラー
public ActionResult InactiveUsers()
{
var users = Membership.GetAllUsers();
return View(users);
}
changeUserPasswordGETおよびPOSTコントローラー
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ビューでRouteLinkをクリックすると、次のようなURLでパスワードのリセットページが表示されます。
http:// localhost:83 / ControlPanel / changeUserPassword / example1@gmail.com、ただし、
RouteLinkをクリックすると、ビューchangeUserPasswordが見つからないというエラーがスローされ、URLは次のようになります。
http:// localhost:83 / ControlPanel / changeUserPassword / example1%40gmail.com -「@」記号がどのように台無しになるかを確認しますか?
また、コードをデバッグしましたが、GET changeUserPasswordで、ユーザー名が正しく入力されています:example1@gmail.comなので、それを台無しにしているのはURLだけだと思いますか?
URLを手動で入力すると、changeUserPasswordビューが表示されますが、パスワードリセット機能が機能しません。'オブジェクトのインスタンスに設定されていないオブジェクト参照'例外は、if(mu.ChangePassword(mu.ResetPassword()、password))行でスローされます。
最初の問題(URL'@'記号の問題)を解決できれば、2番目の問題と一緒に役立つかもしれないと思います。
どんな助けもいただければ幸いです:)
スタックトレース-要求に応じて
ソースエラー:
現在の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