本文整理汇总了C#中ClientContext.ParseObjectFromJsonString方法的典型用法代码示例。如果您正苦于以下问题:C# ClientContext.ParseObjectFromJsonString方法的具体用法?C# ClientContext.ParseObjectFromJsonString怎么用?C# ClientContext.ParseObjectFromJsonString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ClientContext
的用法示例。
在下文中一共展示了ClientContext.ParseObjectFromJsonString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegisterWebAPIService
/// <summary>
/// This method needs to be called from a code behind of the SharePoint app startup page (default.aspx). It registers the calling
/// SharePoint app by calling a specific "Register" api in your WebAPI service.
///
/// Note:
/// Given that method is async you'll need to add the Async="true" page directive to the page that uses this method.
/// </summary>
/// <param name="page">The page object, needed to insert the services token cookie and read the querystring</param>
/// <param name="apiRequest">Route to the "Register" API</param>
/// <param name="serviceEndPoint">Optional Uri to the WebAPI service endpoint. If null then the assumption is taken that the WebAPI is hosted together with the page making this call</param>
public static async void RegisterWebAPIService(this Page page, string apiRequest, Uri serviceEndPoint = null)
{
if (page == null)
throw new ArgumentNullException("page");
if (string.IsNullOrEmpty(apiRequest))
throw new ArgumentNullException("apiRequest");
if (!page.IsPostBack)
{
if (page.Request.QueryString.AsString(SERVICES_TOKEN, string.Empty).Equals(string.Empty))
{
// Construct a JsonWebSecurityToken so we can fetch the cachekey...implementation is copied from tokenhelper approach
string cacheKey = string.Empty;
string contextToken = TokenHelper.GetContextTokenFromRequest(page.Request);
JsonWebSecurityTokenHandler tokenHandler = TokenHelper.CreateJsonWebSecurityTokenHandler();
SecurityToken securityToken = tokenHandler.ReadToken(contextToken);
JsonWebSecurityToken jsonToken = securityToken as JsonWebSecurityToken;
string appctx = GetClaimValue(jsonToken, "appctx");
if (appctx != null)
{
ClientContext ctx = new ClientContext("http://tempuri.org");
Dictionary<string, object> dict = (Dictionary<string, object>)ctx.ParseObjectFromJsonString(appctx);
cacheKey = (string)dict["CacheKey"];
}
// Remove special chars (=, +, /, {}) from cachekey as there's a flaw in CookieHeaderValue when the
// cookie is read. This flaw replaces special chars with a space.
cacheKey = RemoveSpecialCharacters(cacheKey);
bool httpOnly = true;
if (serviceEndPoint != null)
{
if (!serviceEndPoint.Host.Equals(page.Request.Url.Host, StringComparison.InvariantCultureIgnoreCase))
{
httpOnly = false;
}
}
else
{
serviceEndPoint = new Uri(String.Format("{0}://{1}:{2}", page.Request.Url.Scheme, page.Request.Url.Host, page.Request.Url.Port));
}
// Write the cachekey in a cookie
HttpCookie cookie = new HttpCookie(SERVICES_TOKEN)
{
Value = cacheKey,
Secure = true,
HttpOnly = httpOnly,
};
page.Response.AppendCookie(cookie);
//Register the ClientContext
WebAPIContext sharePointServiceContext = new WebAPIContext()
{
CacheKey = cacheKey,
ClientId = TokenHelper.ClientId,
ClientSecret = TokenHelper.ClientSecret,
Token = contextToken,
HostWebUrl = page.Request.QueryString.AsString("SPHostUrl", null),
AppWebUrl = page.Request.QueryString.AsString("SPAppWebUrl", null),
HostedAppHostName = String.Format("{0}:{1}", page.Request.Url.Host, page.Request.Url.Port),
};
using (var client = new HttpClient())
{
client.BaseAddress = serviceEndPoint;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PutAsJsonAsync(apiRequest, sharePointServiceContext);
if (!response.IsSuccessStatusCode)
{
LoggingUtility.Internal.TraceError((int)EventId.ServicesRegistrationFailed, CoreResources.Service_RegistrationFailed, apiRequest, serviceEndPoint.ToString(), cacheKey);
throw new Exception(String.Format("Service registration failed: {0}", response.StatusCode));
}
LoggingUtility.Internal.TraceInformation((int)EventId.ServicesRegistered, CoreResources.Services_Registered, apiRequest, serviceEndPoint.ToString(), cacheKey);
}
}
}
}