Make a Method of the Business Layer secure. best practice / best pattern

后端 未结 3 1878
-上瘾入骨i
-上瘾入骨i 2021-02-06 02:55

We are using ASP.NET with a lot of AJAX \"Page Method\" calls. The WebServices defined in the Page invokes methods from our BusinessLayer. To prevent hackers to call the Page M

3条回答
  •  走了就别回头了
    2021-02-06 03:45

    One "best practice" is to implement Security an aspect. This keeps the security rules separate from the primary business logic, avoiding hard-coding and making it easy to change the security rules in different environments.

    The article below lists 7 ways of implementing aspects and keeping the code separate. One approach that is simple and doesn't change your business logic interface is to use a proxy. This exposes the same interface as you have currently, yet allows an alternative implementation, which can decorate the existing implementation. The security requirements can be injected into this interface, using either hard-coding or custom attributes. The proxy intercepts method calls to your business layer and invokes the appropriate security checks. Implementing interception via proxies is described in detail here - Decouple Components by Injecting Custom Services into your Object's Invocation Chain. Other AOP approaches are given in Understanding AOP in .NET.

    Here's a forum post discussing security as an aspect, with implementation using advice and security attributes. The end result is

    public static class Roles 
    {
        public const string ROLE_ADMIN = "Admin";
        public const string ROLE_CONTENT_MANAGER = "Content Manager";
    }
    
    // business method    
    [Security(Roles.ROLE_HR)]
    public List GetAllEmployees();
    

    You can put the attribute directly on your business method, tight coupling, or create a service proxy with these attributes, so the security details are kept separate.

提交回复
热议问题