ASP.Net MVC 4登录期间角色重定向到相应的页面 [英] ASP.Net MVC 4 Getting Roles during login to redirect to appropriate page

查看:213
本文介绍了ASP.Net MVC 4登录期间角色重定向到相应的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将用户重定向到相应的页面上登录。所以,我检查,看看是否他们中的一个角色,然后重定向基于隶属。然而,Roles.IsUserInRole()似乎不工作。另外,当我使用Roles.GetRolesForUser(用户名)我得到System.String []。我正在使用mvc4默认simplemembership。我可以看到用户在数据库中创建并链接到相应的角色。此外,当我使用[授权角色..]工作正常。这是我的登录(pretty多少与我的角色检查默认的登录和重定向补充说。

I am trying to redirect the user to the appropriate page on login. So I check to see if they are in a role and then redirect based on membership. However, Roles.IsUserInRole() doesn't seem to work. Plus when I use Roles.GetRolesForUser("username") I get "System.String[]". I am using the default simplemembership in mvc4. I can see the users created in the database and are linked to the appropriate roles. Also, when I use the [Authorize Roles..] that works fine. Here is my login(Pretty much the default login with my Role Check and redirect added.

public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {   //Get list of roles to print
                Session["FName"] = Roles.GetRolesForUser(model.UserName);
                if(Roles.IsUserInRole(model.UserName,"User")){
                    return RedirectToAction("Index","UserLanding");
                    Session["FName"] = "User in User group";
                }
                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);
        }

当在用户组中的用户登录他们重定向到UserLanding Congroller索引页我期望的那样。然而,他们总是被重定向到主页。

I would expect when a user logs in in the "user" group they get redirected to the UserLanding Congroller Index page. However, they always get redirected to the Home page.

推荐答案

我猜你不能访问Roles.IsUserInRole或GetRolesForUser直到重定向后,因此空值。我发现这个在基于角色授权rel=\"nofollow\">。如果有人有办法直接访问数据库的角色,不经历角色,我可以使用它。但在此期间,我将只需要实现额外的重定向在文章中建议道。

I guess you can't access Roles.IsUserInRole or GetRolesForUser until after a redirect, hence the empty values. I found this here under role based authorization. If somebody has a way to access the roles directly in the DB without going through Role I could use that. But in the meantime I will just implement the additional redirect as suggested in the article.

这篇关于ASP.Net MVC 4登录期间角色重定向到相应的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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