asp.net-mvc - MVC 4 中的角色何时初始化?不是在成功登录后

标签 asp.net-mvc roles simplemembership

想要设置一些基于角色的依赖项(Ioc、Ninject)。但是成功登录后,用户的角色是未知的。何时何地进行注入(inject)?我可以强制初始化角色吗?还是我必须自己去取?

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
        {
            DoRolebasedObjectBinding(model.UserName);   // THIS IS THE WRONG PLACE!
            return RedirectToLocal(returnUrl);
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return View(model);
    }

应用程序以 HomeContoller 开始(未设置角色),成功登录后,应用程序被重定向到 de HomeController(参见上面的标准代码),现在角色已设置...
一个技巧是通过添加所有角色或额外的“公共(public)”角色来强制在 HomeController 中初始化角色
public class HomeController : Controller
{
    [Authorize(Roles = "Common")]
    public ActionResult Index()
    {
        DoRolebasedObjectBinding(User.Identity.Name);

但这会强制登录,也会强制 HomeController 作为起点。希望是一个“优雅”的地方来调用我的 DoRolebasedObjectBinding。

最佳答案

基于 Role based authentication in the new MVC 4 Internet template ,答案很简单:自己动手:

var roles = Roles.Provider;
string[] rolesArr = roles.GetRolesForUser(username);

关于asp.net-mvc - MVC 4 中的角色何时初始化?不是在成功登录后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13805774/

相关文章:

NetSuite 自定义联系人 -> 角色列表

security - 设计审查 - 基于角色的访问系统的性能和可扩展性问题

c# - MVC4 使用什么加密?

c# - 如何将 IFormFile 传递到我的服务层?

c# - 使用 jQuery Ajax 将对象列表传递给 ActionResult MVC Controller 方法

php - 在 WordPress 中为多个站点创建编辑器

asp.net-mvc-4 - .NET MVC4 SimpleMembership 错误 - "No user found was found that has the name xxx"

asp.net-mvc-4 - SimpleMembershipProvider : webpages_Membership table

c# - Asp.Net MVC 2 - 将模型的属性绑定(bind)到不同的命名值

.net - 如何使用 Visual Basic 将一些 XML 插入到 XDocument 中?