如何重新命名Asp.net身份角色 [英] How rename Asp.net Identity Role

查看:217
本文介绍了如何重新命名Asp.net身份角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加3角色的数据库。 管理版主用户 。我想简单地重命名
管理管理员。我用这个code,但其不正确的工作。它返回我的错误的 {数据库操作预计将影响1行(S),但实际影响0行(S)数据可能已被修改或删除,因为实体被装见的 http://go.microsoft.com/fwlink/?LinkId=527962 对信息的理解和处理乐观并发异常。 }

Edit.cshtml

  @model Microsoft.AspNet.Identity.EntityFramework.IdentityRole
    @using(Html.BeginForm())
    {
        @ Html.AntiForgeryToken()
        @ Html.ValidationSummary(真)
        @ Html.HiddenFor(型号=> model.Id)
        < D​​IV>
            角色名称
        < / DIV>
        &所述p为H.;
            @ Html.TextBoxFor(型号=> model.Name)
        &所述; / P>
        <输入类型=提交值=保存/>
    }

RoleController

  [HttpPost]
    [ValidateAntiForgeryToken]
    公众的ActionResult编辑(IdentityRole角色)// IdentityRole作用
    {
        尝试
        {            context.Entry(角色).STATE = EntityState.Modified;
            context.SaveChanges();            返回RedirectToAction(「指数」);
        }
        赶上(异常前)
        {
            返回查看();
        }
    }


解决方案

使用身份所提供的角色管理器。

在Startup.Auth,确保RoleManager可以是这样引用:

 公共无效ConfigureAuth(IAppBuilder应用程序)
{
    //添加此引用
    app.CreatePerOwinContext< ApplicationRoleManager>(ApplicationRoleManager.Create);
}

请确保您的控制器包括此构造:

 私人ApplicationRoleManager _roleManager;
公共ApplicationRoleManager RoleManager {{返回_roleManager? 。HttpContext.GetOwinContext()获取< ApplicationRoleManager>(); }私人集合{_roleManager =价值; }}

然后就可以用这个,而不是在问题code(给出异步):

  [HttpPost]
[ValidateAntiForgeryToken]
公共异步任务<&的ActionResult GT;编辑(IdentityRole角色)
{
    尝试
    {
        IdentityRole thisRole =等待RoleManager.FindByIdAsync(role.Id);
        thisRole.Name = role.Name;
        等待RoleManager.UpdateAsync(thisRole);
        返回RedirectToAction(「指数」);
    }
    赶上(异常前)
    {
        返回查看();
    }
}

和最后确保你处置角色管理是这样的:

 保护覆盖无效的Dispose(BOOL处置)
{
    如果(处置和放大器;&安培;!RoleManager = NULL)
    {
        RoleManager.Dispose();
        RoleManager = NULL;
    }
    如果(处置)
    {
        context.Dispose();
    }
    base.Dispose(处置);
}

这应该可以做的伎俩。

I add 3 roles to database. "Admin","Moderator" and "User". I want simply rename "Admin" to "Administrator". I use this code, but its not correct work. Its return me error {"Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions."}

Edit.cshtml

@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.Id)
        <div>
            Role name
        </div>
        <p>
            @Html.TextBoxFor(model => model.Name)
        </p>
        <input type="submit" value="Save" />
    }

RoleController

  [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(IdentityRole role) //IdentityRole role
    {
        try
        {

            context.Entry(role).State = EntityState.Modified;
            context.SaveChanges();

            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            return View();
        }
    }

解决方案

Use the Role Manager provided by Identity.

In Startup.Auth, make sure the RoleManager is referenced like this:

public void ConfigureAuth(IAppBuilder app)
{
    // Add this reference
    app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
}

Make sure your Controller includes this constructor:

private ApplicationRoleManager _roleManager;
public ApplicationRoleManager RoleManager { get { return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); } private set { _roleManager = value; } }

Then you can use this instead of the code in the question (given as async):

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(IdentityRole role)
{
    try
    {
        IdentityRole thisRole = await RoleManager.FindByIdAsync(role.Id);
        thisRole.Name = role.Name;
        await RoleManager.UpdateAsync(thisRole);
        return RedirectToAction("Index");
    }
    catch (Exception ex)
    {
        return View();
    }
}

And finally make sure you dispose of the Role Manager like this:

protected override void Dispose(bool disposing)
{
    if (disposing && RoleManager != null)
    {
        RoleManager.Dispose();
        RoleManager = null;
    }
    if (disposing)
    {
        context.Dispose();
    }
    base.Dispose(disposing);
}

Hopefully this should do the trick.

这篇关于如何重新命名Asp.net身份角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆