【问题标题】:Authentication Succeeds when Debugging but Fails on Azure App Service调试时身份验证成功,但在 Azure 应用服务上失败
【发布时间】:2018-05-18 06:11:57
【问题描述】:

我只是通过以下方法使用 CSOM 从 SharePoint 获取一些用户。这一直对我有用,而且我没有遇到任何问题。

突然之间,当我今天尝试调用此方法时,它失败并出现此错误

The sign-in name or password does not match one in the Microsoft account system.
at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.GetServiceToken(String securityXml, String serviceTarget, String servicePolicy)
    at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.GetServiceToken(String username, String password, String serviceTarget, String servicePolicy)
    at Microsoft.SharePoint.Client.Idcrl.SharePointOnlineAuthenticationProvider.GetAuthenticationCookie(Uri url, String username, SecureString password, Boolean alwaysThrowOnFailure, EventHandler`1 executingWebRequest)
    at Microsoft.SharePoint.Client.SharePointOnlineCredentials.GetAuthenticationCookie(Uri url, Boolean refresh, Boolean alwaysThrowOnFailure)
    at Microsoft.SharePoint.Client.ClientRuntimeContext.SetupRequestCredential(ClientRuntimeContext context, HttpWebRequest request)
    at Microsoft.SharePoint.Client.SPWebRequestExecutor.GetRequestStream()
    at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
    at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
    at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
    at SharePointLibrary.SPClient.GetAllUsers() in C:\Users\bassie\source\repos\TFS\ADVWKSP\SharePointLibrary\SPClientUsers.cs:line 39

但只有在发布到 Azure 后才会失败。

我已经记录了用于 Azure 应用程序流的用户名和密码,它们绝对正确,并且在我的机器上调试时使用的用户名和密码相同。

这怎么可能?我要疯了吗?

构造函数

public SPClient(string url)
{
    baseUrl = url;
    var userName = ConfigurationManager.ConnectionStrings["SPsvcUsername"].ConnectionString;
    var password = ConfigurationManager.ConnectionStrings["SPsvcPassword"].ConnectionString;
    Trace.TraceInformation(userName);
    Trace.TraceInformation(password);

    var securePassword = new SecureString();
    foreach (var c in password)
    {
        securePassword.AppendChar(c);
    }
    credentials = new SharePointOnlineCredentials(userName, securePassword);
}

获取用户方法

public IEnumerable<SharePointUser> GetAllUsers()
{
    var spUsers = new List<SharePointUser>();

    using (var clientContext = new ClientContext(baseUrl))
    {
        clientContext.Credentials = credentials;

        var web = clientContext.Web;
        var list = clientContext.Web.SiteUserInfoList;
        var users = list.GetItems(new CamlQuery());
        clientContext.Load(users, includes => includes.Include(
            f => f["GUID"],
            f => f["FirstName"],
            f => f["LastName"],
            f => f["UserName"],
            f => f["Picture"],
            f => f.DisplayName));

        clientContext.ExecuteQuery();

        foreach (var user in users)
        {
            var imagePath = (FieldUrlValue)user.FieldValues["Picture"];
            spUsers.Add(new SharePointUser()
            {
                FirstName = (user.FieldValues["FirstName"] is string firstName) ? firstName : string.Empty,
                LastName = (user.FieldValues["LastName"] is string lastName) ? lastName : string.Empty,
                UserName = (user.FieldValues["UserName"] is string userName) ? userName.ToLower() : string.Empty,
                ImagePath = (user.FieldValues["Picture"] is FieldUrl pictureUrl) ? pictureUrl.ToString() : string.Empty,
                DisplayName = user.DisplayName
            });
        }
    }

    return spUsers;
}

【问题讨论】:

    标签: c# asp.net-mvc azure sharepoint csom


    【解决方案1】:

    由于凭据正确,可能是启用了多重身份验证,并且可能为此帐户触发了策略。如果是这种情况,您可以为该特定帐户禁用 MFA。

    此外,作为PnP Core library 一部分的 AuthenticationManager 类可能是有益的,因为它有助于各种身份验证方案。

    【讨论】:

    • 感谢您的建议。这是可能的,因为 2FA 最近才开启。然而,在这件事发生后,该帐户继续工作了几个星期。那可能吗?这些事情是否存在某种延迟或宽限期?我仍然需要通过从帐户中删除 2FA 或更改应用程序以使用非 2FA 帐户来测试这一点,并且一旦我检查过就会在此处发布(这可能需要一些时间来设置..)
    • 另外,SharePointPnPCoreOnline 看起来很有趣,但它似乎只存在于 .net 核心项目中,对吗?我使用的是标准框架
    • 是的,可能会有延迟。状态可以设置为“Enforced”或“Enabled”。如果设置为“强制”,该应用程序可能会立即停止工作。此外,可以应用条件访问策略,仅在满足指定条件时才需要多因素身份验证。关于 PnP Core 库,它看起来与 .NET Standard 不完全兼容,但在面向 .NET Framework 时确实可以工作。
    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 2017-08-28
    • 2011-11-22
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    相关资源
    最近更新 更多