问题
Is it possible to use SQL's Dynamic data masking with Entity framework?
If it is possible, is there any way to combine it with Asp.Identity? Project I'm working on requires that data is masked for certain user roles and visible to others.
We are using database first approach and Entity framework with data fields masked with:
MASKED WITH (FUNCTION = 'default()')
that need to be visible to admins and remain masked to other user roles. User roles are defined thru Asp.identity.
回答1:
If it suits well your app architecture you can try approach from this blog post this blog post.
In two words you can create special db user which will represent your "other user roles". And then create two instances of DbContext
: one for admin and one for the rest of your roles. So, basically, it's all about user identity provided in connection string.
Correct instance of DbContext with right connection string based on current user you can, for example, provide with dependency injection.
来源:https://stackoverflow.com/questions/52078018/dynamic-data-masking-with-entity-framework