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
Asp.net mvc 给定用户名字符串,如何检查该用户是否为用户名?_Asp.net Mvc_Asp.net Mvc 3_Membership Provider_Isinrole - Fatal编程技术网

Asp.net mvc 给定用户名字符串,如何检查该用户是否为用户名?

Asp.net mvc 给定用户名字符串,如何检查该用户是否为用户名?,asp.net-mvc,asp.net-mvc-3,membership-provider,isinrole,Asp.net Mvc,Asp.net Mvc 3,Membership Provider,Isinrole,我试图在给定用户完全“登录”到我正在构建的站点之前检查他们是否具有角色。通常我会使用以下代码: User.IsInRole("CustomRole") 但在这种情况下,该行总是产生“false”-我相信这是因为要使用User.IsInRole,用户必须已经完全登录。我试图在我的帐户控制器的登录方法中检查这条信息,因此用户尚未登录(我想) 我将如何返回用户对象,以便我可以执行下面尝试执行的操作: public ActionResult LogOn(LogOnModel model, string

我试图在给定用户完全“登录”到我正在构建的站点之前检查他们是否具有角色。通常我会使用以下代码:

User.IsInRole("CustomRole")
但在这种情况下,该行总是产生“false”-我相信这是因为要使用User.IsInRole,用户必须已经完全登录。我试图在我的帐户控制器的登录方法中检查这条信息,因此用户尚未登录(我想)

我将如何返回用户对象,以便我可以执行下面尝试执行的操作:

public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

            // Need to identify the user because the "User" is not officially 'logged in' yet and cannot be accessed via "User.IsInRole" - am I correct in this understanding
            MembershipUser u = Membership.GetUser(model.UserName);
            if (u.IsInRole("Administrator"))

. . . . truncated
上述代码引发以下错误:

'System.Web.Security.MembershipUser' does not contain a definition for 'IsInRole' and no extension method 'IsInRole' accepting a first argument of type 'System.Web.Security.MembershipUser' could be found (are you missing a using directive or an assembly reference?)

MembershipUser u=Membership.GetUser(model.UserName)显然没有返回我可以使用IsInRole的对象,有什么提示吗?

我认为角色和用户名都有过载

你想要的是

var authorized = Roles.IsUserInRole(username, roleName);