我在 web.config 中启用了角色管理器,但仍然收到错误消息:
System.Configuration.Provider.ProviderException :角色管理器功能尚未启用。
我正在使用 nunit,这是我的测试方法
[Test]
public void DownloadbloombergTest()
{
if (!Roles.RoleExists("Admin"))
Roles.CreateRole("Admin");
if (!Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
Roles.AddUserToRole(WebSecurity.CurrentUserName, "Admin");
var controller = new DownloadBloombergController();
var result = controller.DownloadBloomberg() as ViewResult;
Assert.IsInstanceOf<ViewResult>(result);
}
这是我的 web.config
...
<system.web>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear />
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear />
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
</providers>
</roleManager>
<customErrors mode="Off" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Login/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
...
nunit 指向行(方法的开头)
if (!Roles.RoleExists("Admin"))