开始使用ASP.NET Web API中的角色授权属性

时间:2015-03-27 08:24:09

标签: c# asp.net-mvc-4 asp.net-web-api .net-4.0 thinktecture

我有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中的角色授权属性

0 个答案:

没有答案