Asp.Net MVC 5 混合 Windows 身份验证与角色提供者
Asp.Net MVC 5 mixing Windows authentication with a role provider
我正在尝试将 Windows 身份验证与我自己的角色提供者混合使用 - 但我似乎无法让它识别 "IsUserInRole("....","... ")"
我在我的模型 -> 安全文件夹中添加了一个新的 class,名为 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;
}
}
}
在我的 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>
}
User.Identity.Name 应该提供我的 Windows 用户名(确实如此),但是 IsUserInRole
带有红色下划线。
如何让系统识别我的新提供商?
谢谢,
马克
使用扩展方法:
public static bool IsUserInRole(this HtmlHelper helper, string username, string roleName)
{
// your code
}
那么在你看来:
@if(Html.IsUserInRole(userName, Role))
我正在尝试将 Windows 身份验证与我自己的角色提供者混合使用 - 但我似乎无法让它识别 "IsUserInRole("....","... ")"
我在我的模型 -> 安全文件夹中添加了一个新的 class,名为 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;
}
}
}
在我的 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>
}
User.Identity.Name 应该提供我的 Windows 用户名(确实如此),但是 IsUserInRole
带有红色下划线。
如何让系统识别我的新提供商?
谢谢,
马克
使用扩展方法:
public static bool IsUserInRole(this HtmlHelper helper, string username, string roleName)
{
// your code
}
那么在你看来:
@if(Html.IsUserInRole(userName, Role))