首页 > 解决方案 > ASP.Net 核心角色管理器导致站点崩溃的异步方法

问题描述

我一直试图让我的头脑围绕着 ASP.Net Core Identity,虽然我取得了一些成功,但我遇到了一些我无法拼凑起来的问题。

我在我的测试应用程序中创建了一个角色管理页面,其中列出了当前站点角色,并有一个文本框供用户创建新角色。页面加载正常,我为应用程序播种的 1 个角色为用户显示,但我创建新角色的表单在我尝试运行的任何 Async 方法上崩溃。

这是我的 Startup.cs 配置。我希望这段代码没有问题,因为我已经在互联网上看到了这个。

services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
               .AddEntityFrameworkStores<ApplicationDbContext>();

下面是我如何在 Page 模型中初始化我的roleManager 。

private RoleManager<IdentityRole> roleManager;

public RolesModel(RoleManager<IdentityRole> roleManager)
        {
            this.roleManager = roleManager;
        }

这是我添加新角色的 OnPost 方法。我已经编写并重写了它以首先尝试RoleExistsAsyncCreateAsync,但两者都会导致调试器停止处理并直接进入页面渲染,其中模型设置为对象的实例并且页面错误。我不确定为什么错误也没有被catch部分捕获。

public async void OnPost()
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // creates new role from form data
                    IdentityRole role = new IdentityRole(HttpContext.Request.Form["NewRole"]);

                    var exists = await roleManager.RoleExistsAsync(HttpContext.Request.Form["NewRole"]);
                    if (!exists)
                    {
                        var results = await roleManager.CreateAsync(role);

                        if (results.Succeeded)
                        {
                            Message = "Success! Role created.";

                            // binds list of roles
                            BindRoles();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Error = ex.Message;
            }
        }

有什么建议么?

更新:我添加了下面试图发布的 HTML 表单...

<form method="post">
    <table class="table table-responsive">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Normalized Name</th>
                <th>Concurrency Stamp</th>
            </tr>
        </thead>
        <tbody>
            @if (Model.roles != null)
            {
                @if (Model.roles.Count > 0)
                {
                    @foreach (var r in Model.roles)
                    {
                        <tr>
                            <td>@r.Id</td>
                            <td>@r.Name</td>
                            <td>@r.NormalizedName</td>
                            <td>@r.ConcurrencyStamp</td>
                        </tr>

                    }
                }
                else
                {
                    <tr>
                        <td colspan="4">
                            <em>No roles</em>
                        </td>
                    </tr>
                }
            }


        </tbody>
        <tfoot>
            <tr>
                <td colspan="4">
                    <div class="input-group mb-3">
                        <input type="text" class="form-control" placeholder="New Role" aria-label="New Role" id="NewRole" name="NewRole">
                        <div class="input-group-append">
                            <button type="submit" formmethod="post" class="btn btn-primary" href="#" rel="button">Add Role</button>
                        </div>
                    </div>
                </td>
            </tr>
        </tfoot>
    </table>
</form>

标签: c#asp.net-corerazorasp.net-core-identity

解决方案


更新我OnPost的返回方法Task<IActionResult>似乎解决了我的问题。

public async Task<IActionResult> OnPost()

感谢评论区的帮助。


推荐阅读