gpt4 book ai didi

single-sign-on - 重用 ClaimsPrincipal 在线验证 sharepoint

转载 作者:行者123 更新时间:2023-12-02 02:01:00 26 4
gpt4 key购买 nike

我有一个 Office 365 帐户(使用最新的 SharePoint 2013 实例)

我还有一个针对 Office 365 进行身份验证的简单 .net Web 应用程序,我创建了一个 AppPrincipalId 并使用 New-MsolServicePrincipal powershell 命令添加了它。

这工作正常。我启动应用程序(在调试中),它重定向到 365 登录,我登录,它返回到应用程序,我从 ClaimsAuthenticationManager 派生了一个类并覆盖了 Authenticate 方法。

我现在可以看到 ClaimsPrincipal,以及相关的声明和身份等。

现在我想重新使用此身份以编程方式访问 SharePoint。

我的问题:

a) SharePoint 会允许这个身份吗(看它是由 sts.windows.net 颁发的)

b) 我如何重构一个有效的 JWT(或使用现有的),并使用身份验证承载将其封装在 HttpRequest 中。

我正在使用的代码如下 - 这将返回 401 未授权。

如有任何帮助,我们将不胜感激。

public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal)
{
if (incomingPrincipal != null && incomingPrincipal.Identity.IsAuthenticated == true)
{

List<Claim> claims = null;
claims = (from item in incomingPrincipal.Claims
where item.Type.StartsWith("http", StringComparison.InvariantCultureIgnoreCase)
select item).ToList();

RNGCryptoServiceProvider cryptoProvider = new RNGCryptoServiceProvider();
byte[] keyForHmacSha256 = Convert.FromBase64String("Gs8Qc/mAF5seXcGHCUY/kUNELTE=");

// Create our JWT from the session security token
JWTSecurityToken jwt = new JWTSecurityToken
(
"https://sts.windows.net/myAppIdGuid/",
"00000003-0000-0ff1-ce00-000000000000", // sharepoint id
claims,
new SigningCredentials(
new InMemorySymmetricSecurityKey(keyForHmacSha256),
"http://www.w3.org/2001/04/xmldsig-more#hmac-sha256",
"http://www.w3.org/2001/04/xmlenc#sha256"),
DateTime.UtcNow,
DateTime.UtcNow.AddHours(1)
);

var validationParameters = new TokenValidationParameters()
{
AllowedAudience = "00000003-0000-0ff1-ce00-000000000000", // sharepoint id
ValidIssuer = "https://sts.windows.net/myAppIdGuid/", // d3cbe is my app
ValidateExpiration = true,
ValidateNotBefore = true,
ValidateIssuer = true,
ValidateSignature = true,
SigningToken = new BinarySecretSecurityToken(Convert.FromBase64String("mySecretKeyFromPowerShellCommand")),
};

JWTSecurityTokenHandler jwtHandler = new JWTSecurityTokenHandler();
var jwtOnWire = jwtHandler.WriteToken(jwt);
var claimPrincipal = jwtHandler.ValidateToken(jwtOnWire, validationParameters);
JWTSecurityToken parsedJwt = jwtHandler.ReadToken(jwtOnWire) as JWTSecurityToken;

HttpWebRequest endpointRequest =
(HttpWebRequest)HttpWebRequest.Create(
"https://MySharepointOnlineUrl/_api/web/lists");
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.Headers.Add("Authorization",
"Bearer " + parsedJwt.RawData);
HttpWebResponse endpointResponse =
(HttpWebResponse)endpointRequest.GetResponse();

}
}

最佳答案

如果您的方案是关于从远程 Web 应用程序使用 SharePoint Online 数据,您可能希望使用 OAuth 流。您不能自己生成 token 。相反,您请求用户同意访问某些范围(资源+许可)。这两个链接应该有帮助

http://msdn.microsoft.com/en-us/library/office/apps/jj687470(v=office.15).aspx http://jomit.blogspot.com.ar/2013/03/authentication-and-authorization-with.html

关于single-sign-on - 重用 ClaimsPrincipal 在线验证 sharepoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17216907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com