Which code segment should you add at line 19?

You create a Web Part that queries a list.

The Web Part contains the following code segment. (Line numbers are included for reference only.)

01 protected override void Render(HtmlTextWriter writer)
02 {
03 SPUserToken spInToken = GetTheContext(SPContext.Current.Site);
04 using (SPSite aSite = new SPSite(curSiteCtx.ID, spInToken))
05 {
06
07 }
08 }
09 private SPUserToken GetTheContext(SPSite nWeb)
10 {
11 nWeb.CatchAccessDeniedException = false;
12 SPUserToken spToken = null;
13 try
14 {
15 spToken = nWeb.SystemAccount.UserToken;
16 }
17 catch (UnauthorizedAccessException generatedExceptionName)
18 {
19
20 }
21 return spToken;
22 }

You need to ensure that users without permissions to the list can view the contents of the list from the Web Part.

Which code segment should you add at line 19?

You create a Web Part that queries a list.

The Web Part contains the following code segment. (Line numbers are included for reference only.)

01 protected override void Render(HtmlTextWriter writer)
02 {
03 SPUserToken spInToken = GetTheContext(SPContext.Current.Site);
04 using (SPSite aSite = new SPSite(curSiteCtx.ID, spInToken))
05 {
06
07 }
08 }
09 private SPUserToken GetTheContext(SPSite nWeb)
10 {
11 nWeb.CatchAccessDeniedException = false;
12 SPUserToken spToken = null;
13 try
14 {
15 spToken = nWeb.SystemAccount.UserToken;
16 }
17 catch (UnauthorizedAccessException generatedExceptionName)
18 {
19
20 }
21 return spToken;
22 }

You need to ensure that users without permissions to the list can view the contents of the list from the Web Part.

Which code segment should you add at line 19?

A.
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite eSite = new SPSite(nWeb.ID))
{
spToken = nWeb.SystemAccount.UserToken;
}
}

B.
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite eSite = new SPSite(nWeb.ID))
{
spToken = SPContext.Current.Web.CurrentUser.UserToken;
}
}

C.
spToken = nWeb.RootWeb.AllUsers[SPContext.Current.Web.Name].UserToken;

D.
spToken = nWeb.RootWeb.AllUsers[WindowsIdentity.GetCurrent().Name].UserToken;

Explanation:
MNEMONIC RULE: “UnauthorizedAccessException = RunWithElevatedPrivileges = SystemAccount”

Answer A is the only one that will give us a SystemAccount token from within RunWithElevatedPrivileges statement.
That’s what we are trying to get in case UnauthorizedAccessException occurs.



Leave a Reply 0

Your email address will not be published. Required fields are marked *