Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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/0/asp.net-mvc/17.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
C# 如何从数据库添加角色_C#_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 如何从数据库添加角色

C# 如何从数据库添加角色,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,我有我的ASP.NETMVC4项目和数据库(SQLServer2008) 我创建了一个实体框架模型,带有自动生成的模型 在数据库中有一个名为Roles的表(2个字段,Id和name) 有3个角色:管理员、版主、用户 加上客户控制员: public class AccountController : Controller { private korovin_idzEntities db = new korovin_idzEntities(); // // GET: /A

我有我的ASP.NETMVC4项目和数据库(SQLServer2008)

我创建了一个实体框架模型,带有自动生成的模型

在数据库中有一个名为Roles的表(2个字段,Id和name)

有3个角色:管理员、版主、用户

加上客户控制员:

public class AccountController : Controller
{

    private korovin_idzEntities db = new korovin_idzEntities();

    //
    // GET: /Account/LogOn

    public ActionResult LogOn()
    {
        return View();
    }

    //
    // POST: /Account/LogOn

    [HttpPost]
    public ActionResult LogOn(LogOnModel model/*, string returnUrl*/)
    {
        if (ModelState.IsValid)
        {
            var user = db.Users.Where(x => x.username == model.UserName && x.password == model.Password).FirstOrDefault();

            if (user != null)
            {
                user.isRemember = model.RememberMe;
                db.SaveChanges();
                ViewBag.UserName = model.UserName;
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                FormsAuthentication.RedirectFromLoginPage(model.UserName, false); 
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }
        return View(model);
    }
}
在asp.net mvc应用程序中,在何处以及如何初始化角色?我试图检查角色是否存在,并在AccountController中通过rolemanager初始化角色,但我认为这不是一个好的解决方案

是否可以在global.asax.cs中初始化角色

我知道我应该在登录功能中将角色附加到用户


提前感谢:)

这是我的解决方案,我认为有一种存储角色名称的结构,需要初始化此结构,但我错了,在谷歌搜索后,我找到了解决方案:

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        var context = HttpContext.Current;
        if (context.Request.IsAuthenticated)
        {
            string[] roles = LookupRolesForUser(context.User.Identity.Name);
            var newUser = new GenericPrincipal(context.User.Identity, roles);
            context.User = Thread.CurrentPrincipal = newUser;
        }
    }

    #region helper

    private string[] LookupRolesForUser(string userName)
    {
        string[] roles = new string[1];
        CosmosMusic.Models.korovin_idzEntities db = new CosmosMusic.Models.korovin_idzEntities();
        var roleId = db.Users.Where(x => x.username == userName).FirstOrDefault().id_role;
        roles[0] = db.Role.Where(y => y.id_role == roleId).FirstOrDefault().name;

        return roles;
    }
    #endregion

这是一种方法:另一种方法是创建自定义角色提供程序。