0

現在のユーザーがこのアクションを実行する権利を持っているかどうかを確認するメソッドの承認を確認したいと思います。

これが私が配置した属性の例です:

[AttributeUsageAttribute(AttributeTargets.Method)]
public class IsAuthorized : Attribute
{
    public IsAuthorized(Rights right)
    {
        bool isAuthorized = false;

        if (right == Rights.None)
            isAuthorized = true;
        else
        {
            DataAccessLayer.IDAL dal = new DataAccessLayer.DAL();
            string userName = Thread.CurrentPrincipal.Identity.Name;
            Guid userID = dal.GetUserIDFromUserName(userName);

            isAuthorized = dal.HasRight(userID, right.ToString());
        }

        if (!isAuthorized)
            throw new SecurityException("You don't have the rights to perform this action");
    }
}

そして、これは、ユーザーがメソッドにアクセスする権限を持っているかどうかを確認する方法です:

[IsAuthorized(Rights.CreateUserGroup)]
public string Ping()
{
   return "The service is online";
}
4

0 に答える 0