重用claimprincipal对sharepoint进行在线身份验证



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

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

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

我现在可以看到claimprincipal,以及相关的索赔和身份等。

现在我想重新使用这个身份来编程访问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流。您不能自己生成令牌。相反,您要求用户同意访问某些范围(资源+权限)。这两个链接应该有帮助

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

最新更新