繁体   English   中英

C#MVC:用户密码重置控制器:电子邮件地址作为用户名的问题

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM