[英]C# MVC: User Password Reset Controller: Issues with email addresses as usernames
我已經在C#MVC應用程序中編寫了以下用於重置用戶密碼(使用aspnet成員資格api)的代碼,並在示例教程應用程序(MVC音樂商店)中成功進行了測試。 如果您想先閱讀問題描述,請跳到最后。
非活動用戶視圖 (部分視圖)
<%@ 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>
非活動用戶控制器
public ActionResult InactiveUsers()
{
var users = Membership.GetAllUsers();
return View(users);
}
changeUserPassword GET和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'@'符號問題),可能會對第二個問題有所幫助。
任何幫助,將不勝感激 :)
堆棧跟蹤-根據要求
源錯誤:
當前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/
最好使用System.Web.HttpContext.Current.Server.UrlDecode(url1)
。 無論Url是否已編碼,它都會返回原始網址,例如“ http:// localhost:83/ControlPanel/changeUserPassword/example1@gmail.com”,您可以從中獲取userName。
string userName = System.Web.HttpContext.Current.Server.UrlDecode(userName);
RouteTable.Routes.MapRoute(
"AdminPassword", // routename
"ControlPanel/changeUserPassword/" + userName,
new { controller = "ControlPanel", action = "changeUserPassword", username = UrlParameter.Optional }
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.