我正在寻找一个 NuGet 包,提供类似于 Rails 中 CanCan gem 的功能 (https://github.com/ryanb/cancan)。
有人知道有哪些插件提供了类似的功能吗?或者有简单的实现方式吗?
谢谢
我正在寻找一个 NuGet 包,提供类似于 Rails 中 CanCan gem 的功能 (https://github.com/ryanb/cancan)。
有人知道有哪些插件提供了类似的功能吗?或者有简单的实现方式吗?
谢谢
我最终查看了http://www.develop.com/wifclaimsbasedauthorizationone,它与CanCan非常相似。
例如:
ClaimsPrincipalPermission.CheckAccess("Customer","Add");
检查用户是否有添加客户的权限。
我们正在测试http://thinktecture.github.com/Thinktecture.IdentityModel.45/
基本上是面向 .Net 的基于声明的授权。
随着 MVC5 和 One ASP.Net,声明已经内置到 .Net 的核心中。
经过漫长的搜索,我发现以下文章很有用:
http://msdn.microsoft.com/en-us/library/ff359101.aspx
http://www.codeproject.com/Articles/639458/Claims-Based-Authentication-and-Authorization
http://www.codetails.com/punitganshani/using-claims-identity-with-simplemembership-in-asp-net-mvc/20130525
http://leastprivilege.com/
http://www.postsharp.net/aspects/examples/security
更新
最新的Microsoft在2013年发布的内容:http://blogs.msdn.com/b/webdev/archive/2013/06/27/introducing-asp-net-identity-membership-system-for-asp-net-applications.aspx
示例:
https://dev59.com/W2Ml5IYBdhLWcg3wDzVG#18751036
https://github.com/rustd/AspnetIdentitySample
http://msdn.microsoft.com/en-us/library/hh377151.aspx
我更喜欢CodeProject教程中使用的基于Thinktecture框架的方法,源代码可在以下链接中找到:
https://github.com/brockallen/BrockAllen.MembershipReboot
https://github.com/thinktecture/Thinktecture.IdentityModel.45
请记住,从持久性的角度来看,CodeProject文章已经过时。
现在MembershipReboot支持EntityFramework、MongoDB和RavenDB作为数据存储。
最近,我在搜索与活动基础授权相关的内容时,发现了一些有趣的教程,介绍如何实现它:https://mkarczewski.wordpress.com/2013/10/21/activity-based-authorization-in-modular-systems/
我还发现了这个库,看起来非常酷!这正是我希望找到的东西。https://github.com/michelgrootjans/CanI/blob/master/README.md
Authorize
属性。请查看 ASP.NET Core 文档中的 此页面。它与 cancan 的功能有些相似。
您可以像下面这样编写授权处理程序:
public class DocumentAuthorizationHandler :
AuthorizationHandler<OperationAuthorizationRequirement, Document>
{
public override Task HandleRequirementAsync(AuthorizationHandlerContext context,
OperationAuthorizationRequirement requirement,
Document resource)
{
// Validate the operation using the resource, the identity and
// the Name property value from the requirement.
return Task.CompletedTask;
}
}
现在你可以在你的控制器中使用以下代码:
if (await authorizationService.AuthorizeAsync(User, document, Operations.Read))
{
return View(document);
}
else
{
return new ChallengeResult();
}
或者在你的观点中:
@if (await AuthorizationService.AuthorizeAsync(User, Model, Operations.Edit))
{
<p><a class="btn btn-default" role="button"
href="@Url.Action("Edit", "Document", new { id = Model.Id })">Edit</a></p>
}