Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC管理员重置用户密码_Asp.net Mvc - Fatal编程技术网

Asp.net mvc MVC管理员重置用户密码

Asp.net mvc MVC管理员重置用户密码,asp.net-mvc,Asp.net Mvc,经过一番周折,在我的帮助下,我终于成功了 在我的系统上,当管理员用户登录时,会出现一个链接,供他们进入用户管理系统。这提供了一个用户列表,可以创建另一个用户、删除他们或更改他们的详细信息 此外,当任何用户登录时,他们都可以更改自己的密码。但是,如果用户忘记了密码,管理员用户必须重置密码。我没有给他们发邮件链接或任何花哨的东西。在管理屏幕的“列出用户”位中,有一个操作列,包含编辑、删除、显示详细信息和重置密码的链接 我有一个ApplicationUsersController,它包含编辑、删除等功

经过一番周折,在我的帮助下,我终于成功了

在我的系统上,当管理员用户登录时,会出现一个链接,供他们进入用户管理系统。这提供了一个用户列表,可以创建另一个用户、删除他们或更改他们的详细信息

此外,当任何用户登录时,他们都可以更改自己的密码。但是,如果用户忘记了密码,管理员用户必须重置密码。我没有给他们发邮件链接或任何花哨的东西。在管理屏幕的“列出用户”位中,有一个操作列,包含编辑、删除、显示详细信息和重置密码的链接

我有一个ApplicationUsersController,它包含编辑、删除等功能。我有一系列ApplicationUsers视图,称为创建、编辑、删除、详细信息、编辑、索引。大部分代码是在我创建ApplicationUsersController并选择创建视图时生成的。还有一个ResetUserPasswordsViewModel。以下是ResetPassword视图:

@model ICWeb.Models.ResetUserPasswordViewModel

@{
    ViewBag.Title = "Reset User Password";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@ViewBag.Title</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "Please fix the errors displayed", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.NewPassword, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.NewPassword, new { htmlAttributes = new { @class = "form-control", @autofocus = "autofocus" } })
                @Html.ValidationMessageFor(model => model.NewPassword, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Reset Password" class="btn btn-default" />
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

非常感谢您的帮助。

在我正在开发(但尚未完成或测试)的系统中,我已经编写了这篇文章。它是有效的,所以应该是一个很好的起点。请注意,视图模型会处理密码不匹配的问题,因此您已经了解了这一点

我对用户管理器使用直接注入-只要用您自己的实例替换我的_userManager,不管您如何创建它

@model Models.ResetPasswordViewModel
@{
    ViewBag.Title = "Reset password";
}

<div class="container">
    @if (ViewBag.Error != null)
    {
        <div class="alert-danger mb-2">Error(s) occured : @ViewBag.Error</div>
        @Html.ActionLink("Back to List", "AllUsers", null, new { @class = "btn btn-outline-primary" })
    }
    else
    {
        using (Html.BeginForm("ResetPassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
            @Html.AntiForgeryToken()

            @Html.ValidationSummary("", new { @class = "text-danger" })

            @Html.HiddenFor(x => x.Id)

            <div class="form-group">
                @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label" })
                @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", @readonly = "" } })
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.Password, new { @class = "control-label" })
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
            </div>

            <div class="form-group d-flex">
                @Html.ActionLink("Back to User Edit", "EditUser", "Account", new { userId = Model.Id }, new { @class = "btn btn-outline-primary" })

                <input type="submit" value="Reset Password" class="btn btn-primary ml-auto" />
            </div>
        }
    }
</div>


  public class ResetPasswordViewModel
    {
        [Display(Name = "User Id")]
        public string Id { get; set; }

        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }


[ValidateAntiForgeryToken]
public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var user = _userManager.FindById(model.Id);

    IdentityResult result = null;

    if (user != null)
    {
        string code = await _userManager.GeneratePasswordResetTokenAsync(user.Id);

        result = await _userManager.ResetPasswordAsync(user.Id, code, model.Password);

        if (result.Succeeded)
        {
            return RedirectToAction("ResetPasswordConfirmation", "Account", model);
        }
    }

    // return errors
    var s = new System.Text.StringBuilder();
    //
    foreach (var e in result.Errors)
    {
        if (s.Length > 0)
        {
            s.Append(", ");
        }

        s.Append(e);
    }

    ViewBag.Error = s.ToString();

    return View();
}
@model Models.ResetPasswordViewModel
@{
ViewBag.Title=“重置密码”;
}
@如果(ViewBag.Error!=null)
{
发生错误:@ViewBag.Error
@ActionLink(“返回列表”,“AllUsers”,null,新{@class=“btn btn outline primary”})
}
其他的
{
使用(Html.BeginForm(“ResetPassword”,“Account”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(“,new{@class=“text danger”})
@Html.HiddenFor(x=>x.Id)
@LabelFor(model=>model.UserName,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.UserName,new{htmlAttributes=new{@class=“form control”,@readonly=”“})
@LabelFor(m=>m.Password,新的{@class=“control label”})
@Html.PasswordFor(m=>m.Password,新的{@class=“form control”})
@LabelFor(m=>m.ConfirmPassword,新的{@class=“control label”})
@Html.PasswordFor(m=>m.ConfirmPassword,new{@class=“form control”})
@ActionLink(“返回到用户编辑”,“编辑用户”,“帐户”,新的{userId=Model.Id},新的{@class=“btn btn outline primary”})
}
}
公共类ResetPasswordViewModel
{
[显示(名称=“用户Id”)]
公共字符串Id{get;set;}
[显示(Name=“User Name”)]
公共字符串用户名{get;set;}
[必需]
[StringLength(100,ErrorMessage={0}的长度必须至少为{2}个字符。”,MinimumLength=6)]
[数据类型(数据类型.密码)]
[显示(Name=“密码”)]
公共字符串密码{get;set;}
[数据类型(数据类型.密码)]
[显示(Name=“确认密码”)]
[比较(“密码”,ErrorMessage=“密码和确认密码不匹配。”)]
公共字符串ConfirmPassword{get;set;}
}
[ValidateAntiForgeryToken]
公共异步任务ResetPassword(ResetPasswordViewModel模型)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
var user=\u userManager.FindById(model.Id);
IdentityResult结果=null;
如果(用户!=null)
{
字符串代码=wait _userManager.GeneratePasswordResetTokenAsync(user.Id);
result=wait\u userManager.ResetPasswordAsync(user.Id、code、model.Password);
if(result.successed)
{
返回重定向操作(“ResetPasswordConfirmation”,“Account”,model);
}
}
//返回错误
var s=new System.Text.StringBuilder();
//
foreach(result.Errors中的变量e)
{
如果(s.长度>0)
{
s、 附加(“,”);
}
s、 附加(e);
}
ViewBag.Error=s.ToString();
返回视图();
}

那么您可能需要一个viewmodel对象,它包含这些字段吗?另外,为什么要将
id
作为一个单独的参数?当然这已经是模型的一部分了?id可能是我尝试过的东西的宿醉。我把它拿走了。我添加了一个ResetUserPasswordViewModel类。它包含Id属性、密码和confirmpassword属性。那么…它解决了您的问题吗?没有。我更改了视图以使用开头行中的ApplicationUsersModels,其中包含id、newpassword和confirmpassword属性。httppost函数ResetPassword显然是错误的。你有什么想法吗?我只想在AspNetUsers中为所选用户重置密码。用户管理页面列出了我的用户,重置密码链接的格式为:ApplicationUsers/ResetPassword/d1b8e05e-07df-4276-95e6-959b6c6a2b99,因此Id将传递到重置密码页面。帖子还需要接受新的viewmodel作为参数。是你干的吗?也许用最新的代码和当前的问题更新你的问题。仅仅根据描述很难确定。因为我是新手,我不确定我是否理解你所做的。在视图中,我看到您有x、m和模型。你是怎么拿到3分的?陡峭的学习曲线这是:)我在网上学习一门MVC课程,但你不能真正地问它问题,所以你先四处寻找,尝试一些东西。如果你不能得到任何进一步的我来这里问。代码内的br
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ICWeb.Models
{
    public class ResetUserPasswordViewModel
    {
        public string Id { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
}
@model Models.ResetPasswordViewModel
@{
    ViewBag.Title = "Reset password";
}

<div class="container">
    @if (ViewBag.Error != null)
    {
        <div class="alert-danger mb-2">Error(s) occured : @ViewBag.Error</div>
        @Html.ActionLink("Back to List", "AllUsers", null, new { @class = "btn btn-outline-primary" })
    }
    else
    {
        using (Html.BeginForm("ResetPassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
            @Html.AntiForgeryToken()

            @Html.ValidationSummary("", new { @class = "text-danger" })

            @Html.HiddenFor(x => x.Id)

            <div class="form-group">
                @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label" })
                @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", @readonly = "" } })
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.Password, new { @class = "control-label" })
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
            </div>

            <div class="form-group d-flex">
                @Html.ActionLink("Back to User Edit", "EditUser", "Account", new { userId = Model.Id }, new { @class = "btn btn-outline-primary" })

                <input type="submit" value="Reset Password" class="btn btn-primary ml-auto" />
            </div>
        }
    }
</div>


  public class ResetPasswordViewModel
    {
        [Display(Name = "User Id")]
        public string Id { get; set; }

        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }


[ValidateAntiForgeryToken]
public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var user = _userManager.FindById(model.Id);

    IdentityResult result = null;

    if (user != null)
    {
        string code = await _userManager.GeneratePasswordResetTokenAsync(user.Id);

        result = await _userManager.ResetPasswordAsync(user.Id, code, model.Password);

        if (result.Succeeded)
        {
            return RedirectToAction("ResetPasswordConfirmation", "Account", model);
        }
    }

    // return errors
    var s = new System.Text.StringBuilder();
    //
    foreach (var e in result.Errors)
    {
        if (s.Length > 0)
        {
            s.Append(", ");
        }

        s.Append(e);
    }

    ViewBag.Error = s.ToString();

    return View();
}