我有ASP.NET MVC4应用程序项目。我还通过创建ApiContoller
将WebApi添加到我的项目中。我有用于MVC的表单身份验证和用于Web API的基本身份验证(Thinktecture)。
我注意到在ApiContoller中[Authorize]
运行良好,但[Authorize(Roles="")]
永远不会调用方法。我认为原因是在MVC Contoller
后代中,两个语句User.IsInRole("");
和Roles.IsUserInRole(User.Identity.Name, "");
都返回true
,但在ApiContoller
后代中,第一个语句始终是{{1} },如果用户有角色,则第二次返回false
:
true
这是我的web.config配置:
bool booool1 = User.IsInRole("Radiolog");
bool booool2 = Roles.IsUserInRole(User.Identity.Name, "Radiolog");
在 <add key="enableSimpleMembership" value="false" />
<add key="autoFormsAuthentication" value="false" />
...
<roleManager cacheRolesInCookie="false" defaultProvider="CustomRoleProvider" enabled="true">
<providers>
<clear />
<add name="CustomRoleProvider" type="RisSystem.Services.CustomRoleProvider" />
</providers>
</roleManager>
...
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
方法中,我使用ApiController
(Thinktecture)和client.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue(login, password);
中的FormsAuthentication
进行身份验证。
WebApi身份验证在Contoller
函数中的WebApiConfig.cs
中设置:
Register(HttpConfiguration config)
问:如何开始工作使用ASP.NET Web API中的角色授权属性