Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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# Asp.NETMVC5混合Windows身份验证和角色提供程序_C#_Asp.net_Asp.net Mvc_Entity Framework_Asp.net Mvc 5 - Fatal编程技术网

C# Asp.NETMVC5混合Windows身份验证和角色提供程序

C# Asp.NETMVC5混合Windows身份验证和角色提供程序,c#,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我试图将Windows身份验证与我自己的角色提供程序混合使用,但我似乎无法让它识别“IsUserInRole(“…”,“…”)” 我在“模型->安全”文件夹中添加了一个名为MTRoleProvider的新类: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; namespace wb.Models.Security {

我试图将Windows身份验证与我自己的角色提供程序混合使用,但我似乎无法让它识别“IsUserInRole(“…”,“…”)”

我在“模型->安全”文件夹中添加了一个名为MTRoleProvider的新类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace wb.Models.Security 
{
public class MTRoleProvider : RoleProvider
{
    private BoardContext db = new BoardContext();

    public override string[] GetRolesForUser(string username)
    {
        var roleNames = db.UserRole.Where(x => x.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase)).Select(x => x.Role);

        if (roleNames.Count() > 0)
            return roleNames.ToArray();
        else
            return new string[] { }; 
    }


    public override bool IsUserInRole(string username, string roleName)
    {
        var user = db.UserRole.Where(x => x.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase) && x.Role.Equals(roleName,StringComparison.CurrentCultureIgnoreCase));

        if (user != null)
                return true;
            else
                return false;
    }

}
}
在my web.config中-我将此添加到system.web:

<roleManager cacheRolesInCookie="true" defaultProvider="MTRoleProvider" enabled="true">
  <providers>
    <clear />
    <add name="MTRoleProvider" type="wb.Models.Security.MTRoleProvider" />
  </providers>
</roleManager>

我还补充说:

<authentication mode="Windows" />

然后在我的_layout.cshtml文件中,我尝试如下使用它:

 @if(IsUserInRole(User.Identity.Name,"Admin"))
   {
      <text>Admin mode</text>
   }
@if(IsUserInRole(User.Identity.Name,“Admin”))
{
管理模式
}
User.Identity.Name应该给出我的Windows用户名(确实如此),但是
IsUserInRole
用红色下划线

如何让系统识别我的新供应商

谢谢,

标记

使用扩展方法:

 public static bool IsUserInRole(this HtmlHelper helper, string username, string roleName)
    {
       // your code
    }
那么在你看来,

@if(Html.IsUserInRole(userName, Role))

是不是
角色.IsUserInRole
?视图中使用的方法“IsUserInRole”在哪里定义?对于RoleProvider,我希望您只需使用“if(User.IsInRole(“Admin”)”。如果您的RoleProvider配置正确(看起来是这样的话),
User
应该是
RolePrincipal
的一个实例,它包含自定义RoleProvider.Hi提供的角色-它在MTRoleProvider.cs文件中定义-在Models->Security文件夹中-thankscheck this Hi Hi Malkam-它看起来与我的代码完全一样-但即使代码中的断点位于IsUserInRole-它也不是g点击。谢谢,MarkHi Mike-谢谢-我应该在哪里添加扩展方法的代码?在你的MTRoleProvider类中。确保它也是静态的。还要在你的视图中添加@using wb.Models.Security.MTRoleProvider Hi-如果我这样做了,它就不会通过在MTRoleProvider类/代码顶部定义的db上下文识别“db”部分用红色下划线。私有静态BoardContext db=new BoardContext();