Oauth 2.0 SharePoint应用程序中的Oauth2和访问令牌
我正在SharePoint-O365上创建一个内部网,在那里我可以创建一个小部件,在那里我需要提取日历事件并显示它们一周。以下是一个步骤: a。用户登录到Intranet B生成访问令牌以访问Office 365 REST API C获取并显示日历事件 我的问题是: 我想到了两个选项来生成访问令牌 选项a:创建一个WCF应用程序,该应用程序接受用户上下文并生成令牌。这将获取结果并更新列表。我的intranet应用程序可以读取日历列表并更新小部件。这不起作用,因为我无法将用户上下文从SP传递到WCF方法,以便生成访问令牌 选项b:使用下面的代码(我现在已经完成了),但它在URL中显示访问令牌,这对客户端不好 var clientId='>>样本>>'Oauth 2.0 SharePoint应用程序中的Oauth2和访问令牌,oauth-2.0,sharepoint-2013,office365,access-token,Oauth 2.0,Sharepoint 2013,Office365,Access Token,我正在SharePoint-O365上创建一个内部网,在那里我可以创建一个小部件,在那里我需要提取日历事件并显示它们一周。以下是一个步骤: a。用户登录到Intranet B生成访问令牌以访问Office 365 REST API C获取并显示日历事件 我的问题是: 我想到了两个选项来生成访问令牌 选项a:创建一个WCF应用程序,该应用程序接受用户上下文并生成令牌。这将获取结果并更新列表。我的intranet应用程序可以读取日历列表并更新小部件。这不起作用,因为我无法将用户上下文从SP传递到WC
var replyUrl = '<<>>';
var endpointUrl = 'https://outlook.office365.com/api/v1.0/me/events';
var resource = "https://outlook.office365.com/";
var authServer = 'https://login.windows.net/common/oauth2/authorize?';
var responseType = 'token';
var url = authServer +
"response_type=" + encodeURI(responseType) + "&" +
"client_id=" + encodeURI(clientId) + "&" +
"resource=" + encodeURI(resource) + "&" +
"redirect_uri=" + encodeURI(replyUrl);
var replyUrl='';
var endpointUrl='1〕https://outlook.office365.com/api/v1.0/me/events';
变量资源=”https://outlook.office365.com/";
var authServer=https://login.windows.net/common/oauth2/authorize?';
var-responseType='token';
var url=authServer+
“response_type=“+encodeURI(responseType)+”和“+
“client_id=“+encodeURI(clientId)+”和“+
“resource=“+encodeURI(资源)+”和“+
“redirect_uri=“+encodeURI(replyUrl));
window.location=url
那么有没有其他方法可以达到这个目的呢
Ankush既然您提到要使用WCF,您是否正在开发提供的主机SharePoint应用程序 如果我理解正确,我们可以使用显式授权代码授权流,该流没有向用户代理公开访问令牌。下图说明了授权代码授予流程: 以下是检索Office 365资源访问令牌的核心代码,供您参考:
var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new ADALTokenCache(signInUserId));
try
{
DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId,
new ClientCredential(SettingsHelper.ClientId,
SettingsHelper.ClientSecret),
new UserIdentifier(userObjectId,
UserIdentifierType.UniqueId));
string token= authResult.AccessToken;
return authResult.AccessToken;
});
var dcr = await discClient.DiscoverCapabilityAsync(capabilityName);
return new OutlookServicesClient(dcr.ServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId,
new ClientCredential(SettingsHelper.ClientId,
SettingsHelper.ClientSecret),
new UserIdentifier(userObjectId,
UserIdentifierType.UniqueId));
return authResult.AccessToken;
});
}
您可以参考的完整代码示例。是讨论显式和隐含身份验证流之间区别的有用链接。既然您提到要使用WCF,您是否正在开发提供的主机SharePoint应用程序 如果我理解正确,我们可以使用显式授权代码授权流,该流没有向用户代理公开访问令牌。下图说明了授权代码授予流程: 以下是检索Office 365资源访问令牌的核心代码,供您参考:
var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new ADALTokenCache(signInUserId));
try
{
DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId,
new ClientCredential(SettingsHelper.ClientId,
SettingsHelper.ClientSecret),
new UserIdentifier(userObjectId,
UserIdentifierType.UniqueId));
string token= authResult.AccessToken;
return authResult.AccessToken;
});
var dcr = await discClient.DiscoverCapabilityAsync(capabilityName);
return new OutlookServicesClient(dcr.ServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId,
new ClientCredential(SettingsHelper.ClientId,
SettingsHelper.ClientSecret),
new UserIdentifier(userObjectId,
UserIdentifierType.UniqueId));
return authResult.AccessToken;
});
}
您可以参考的完整代码示例。是讨论显式和隐含身份验证流之间区别的有用链接。谢谢。不,基本上我想通过在SharePOint页面中嵌入一个可以调用WCF方法的javascript来实现这一点。谢谢。不,基本上我想通过在SharePOint页面中嵌入javascript来实现这一点,该页面可以调用WCF方法。