Problem: How to check whether
the access of certain objects or schema as a specific user is valid?
Solution:
EXECUTE
AS
Sets the execution context of a session.
By default, a session starts when a user logs in and ends when
the user logs off. All operations during a session are subject to permission
checks against that user. When an EXECUTE AS statement is run,
the execution context of the session is switched to the specified login or user
name. After the context switch, permissions are checked against the login and
user security tokens for that account instead of the person calling the EXECUTE
AS statement. The user or login account is impersonated for the
duration of the session or module execution, or the context switch is
explicitly reverted.
Syntax:
{ EXEC | EXECUTE } AS <context_specification>
[;]
<context_specification>::=
{ LOGIN | USER } = 'name'
[ WITH { NO
REVERT | COOKIE INTO @varbinary_variable } ]
| CALLER
Example:
In this example, I would like to show we can check whether a
user can execute a procedure.
EXECUTE AS user
= 'testuser'
EXECUTE [dbo].[SelectTableB]
REVERT
Results:
Example 2:
In this example, I would like to show how we can create a procedure to execute
as the user level permission
--
to execute procedure
CREATE PROCEDURE [dbo].[SelectTableB]
WITH EXECUTE AS
CALLER
AS
BEGIN
Select * FROM
dbo.TableB
END
0 comments:
Post a Comment