300 likes | 414 Views
SharePointintersection Session SP28 App Identity. Paul Schaeflein paul@schaeflein.net. About Me. Solutions Architect Trainer Hockey fan http://www.schaeflein.net/blog. Agenda. Alternate Security Context Identities in SharePoint page requests Elevation Impersonation
E N D
SharePointintersectionSession SP28App Identity Paul Schaeflein paul@schaeflein.net
About Me • Solutions Architect • Trainer • Hockey fan • http://www.schaeflein.net/blog
Agenda • Alternate Security Context • Identities in SharePoint page requests • Elevation • Impersonation • SP2013 App-model AuthN
An alternate security context • Perform operation on an object to which the current user does not have necessary permissions • Update a shared resource • Defer operation until later time
Elevation versus Impersonation Elevation of Privilege Impersonation Perform actions on behalf of another user • Programmatically perform actions in code using an increased level of privilege
Identities • Process Identity (w3wp.exe) • Set on Application Pool
Identities • Thread Identity • Configured in web.config<identityimpersonate="true"/>
Identities • HttpContext Identity • Configured in web.config <authenticationmode="Forms"/> • SPClaimsAuthMemberhipProvider
Identities • SharePoint identity • SPWeb.SPUser
Elevation of Privilege • How to elevate • Effects of elevation • When to elevate
Create a new context using (SPSitesite =new SPSite(url)){ using (SPWebweb = site.OpenWeb()){ // perform privileged operation } }
Do Not Mix Contexts • Referring to objects created prior to elevating is a worst practice!
Elevation Best Practices • Use elevated privileges to access non-SharePoint resources • AppPool identity must have necessary permissions • Ensure a new context is established
Impersonation • How to impersonate • Effects of impersonation • When to elevate
Impersonate a specific user • Retrieve and store user token • The user token is a byte array SPUser user = SPContext.Current.Web.CurrentUser; SPUserToken toke n = user.UserToken; // store token
Impersonate a specific user • Load and use user token SPUserToken token = RetrieveToken();// a custom method string url = "http://localhost";// use your url using (SPSite site = new SPSite(url, token)) { // access the SPSite and its objects under // the identity represented by the token }
Impersonate the system account • Retrieve and use token SPUserToken token = SPContext.Current.Site.SystemAccount.UserToken;using (SPSite site = new SPSite(url, token)) { // access the SPSite and its objects under // the identity represented by the token }
Impersonation Best Practices • If the code is expected to honor the permissions of the requesting user, the user’s token must be used to perform the impersonation. • Retrieve and store token • Tokens valid for 24 hours • Can be changed via STSADM
Impersonation Best Practices • For code that updates SharePoint on behalf of a user without permissions, use the System Account token to perform the impersonation • If the current user cannot access the system account token, use the RWEP method to retrieve the token.
SharePoint 2013 App AuthN Call is to an app web? User only context Start User credentials provided? No Yes Yes No Yes Yes App token provided? App t oken Includes user? User + App context App only context No No App only context Anonymous context End
App Only Token protectedvoidPage_Load(object sender, EventArgs e) { UrihostWebUri= newUri(Request.QueryString["SPHostUrl"]); stringaccessToken= GetAccessToken(hostWebUri); } privatestringGetAccessToken(UrihostWebUri) { stringtoken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity( hostWebUri, null); returntoken; }
App Identity Online & On-Prem In the cloud Windows Azure AD comes with O365 Apps use ‘3-legged’ Oauth Azure AD App Office 365 Cloud
App Identity Online & On-Prem In the cloud Windows Azure AD comes with O365 Apps use ‘3-legged’ Oauth On-Premises Cert-based trust On-prem to on-prem Azure AD App Office 365 Cloud App SharePoint On-Prem
App Identity Online & On-Prem In the cloud Windows Azure AD comes with O365 Apps use ‘3-legged’ Oauth On-Premises Cert-based trust On-prem to on-prem Hybrid Use O365 Azure AD with on-premSP Supports Marketplace and on-prem apps Azure AD App Office 365 Cloud App SharePoint On-Prem
Don’t forget to enter your evaluation of this session using EventBoard! Thank you! Questions?