Single sign on 重用ClaimsPrincipal以针对sharepoint online进行身份验证

Single sign on 重用ClaimsPrincipal以针对sharepoint online进行身份验证,single-sign-on,windows-identity,office365-apps,Single Sign On,Windows Identity,Office365 Apps,我有一个使用最新SharePoint 2013实例的Office 365帐户 我还有一个简单的.net web应用程序,正在对Office 365进行身份验证。我创建了一个AppPrincipalId,并使用新的MsolServicePrincipal powershell命令添加了它 这是正确的。我在调试中启动应用程序,它重定向到365登录,我登录,它返回到应用程序,我从ClaimsAuthenticationManager派生了一个类并重写了Authenticate方法 我现在可以看到索赔单

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

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

这是正确的。我在调试中启动应用程序,它重定向到365登录,我登录,它返回到应用程序,我从ClaimsAuthenticationManager派生了一个类并重写了Authenticate方法

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

现在,我想重新使用此标识以编程方式访问SharePoint

我的问题是:

SharePoint是否允许此标识,因为它是由sts.windows.net颁发的

b我如何重建一个有效的JWT或使用现有的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流。您不能自己生成令牌。而是请求用户同意访问某些作用域资源+权限。这两个链接应该会有所帮助


Matias-你为我指明了正确的方向!我现在有一个工作原型,非常感谢