服务器端使用自定义属性C#进行许可证验证 [英] License validation in server side using custom attributes C#

查看:86
本文介绍了服务器端使用自定义属性C#进行许可证验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的应用程序服务器端验证许可证有问题。

我想要的是方法

Hi,
I have a problem with validation of License in the server side of our application.
What I want is when the method

CreateProject(User user)

被调用

如果用户有创建新项目的许可,将导致检查许可证服务的幕后情况,以防用户没有允许导致例外的许可。

如果我正确的方法用c#的机制命名:

is invoked
Behind the scenes that will lead to check with the License service if the user has license to create new project, in case the user has no permission that will lead to exception.
If I'm right there way to do it with c#'s mechanism named:

ValidationAttribute



我是对的吗?如果是的话,该怎么办?如果没有,有什么正确的方法?



我的尝试:



当我尝试编写一个小程序时,它不起作用。

程序如下:



MyValidationAttribute属性定义:




Am i right? If yes, how to do it? If no, what the right way?

What I have tried:

When I've tried to write a small program it's not working.
the program as below:

The MyValidationAttribute attribute definition:

[AttributeUsage(AttributeTargets.Method)]
public class MyValidationAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        int id = (int)value;
        if (id > 0)
            return true;
        throw new Exception("Error");
    }
}





要验证的服务:



The service to validate:

public class Service
  {
      [MyValidation]
      public bool GetService(int id)
      {
          if (id > 100)
          {
              return true;
          }
          return false;
      }
  }





主要使用此服务:



The main uses this service:

static void Main(string[] args)
       {
           Service service = new Service();
           service.GetService(-8);
       }





但即使使用否定ID,服务也会抛出异常。

谢谢!



But even with negative id the service does non throws exception.
Thanks!

推荐答案

GetService只返回true或false。没别了。
GetService is just returning true or false. Nothing else.


这篇关于服务器端使用自定义属性C#进行许可证验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆