【问题标题】:How to get username from authentication prompt popup如何从身份验证提示弹出窗口中获取用户名
【发布时间】:2022-01-14 17:37:23
【问题描述】:

我有 SharePoint Online 经典页面,其中实现了一些自定义 JavaScript 和 jQuery 脚本以及需要一些基本身份验证的第三方 .NET 应用程序。当用户访问该页面时,系统会提示他输入用户名和密码。如何从此提示中获取用户名?提示是标准的,看起来像这样(图片不是我的)

【问题讨论】:

    标签: javascript jquery sharepoint basic-authentication


    【解决方案1】:

    您可以使用 SharePoint 的 javascript API 来检索当前用户的信息。

    _spPageContextInfo.userId
    

    见:https://social.technet.microsoft.com/wiki/contents/articles/29766.sharepoint-understanding-the-sppagecontextinfo-object.aspx

    另一个建议是使用 API:

    <script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(getCurrentUser, "sp.js");
     
    var currentUser;
     
    function getCurrentUser(){
    var ctx= new SP.ClientContext.get_current();
    var web = ctx.get_web();
    currentUser = web.get_currentUser();
    ctx.load(currentUser);
    ctx.executeQueryAsync(onSuccess, onFailure);
    }
     
    function onSuccess() {
    alert(currentUser.get_title()); // Domain\Account 
    alert(currentUser.get_email());
    document.getElementById('userLogin').innerHTML = currentUser.get_loginName(); 
    }
     
    function onFailure() {
    alert('request failed' + args.get_message() + '\n' + args.get_stackTrace());
    }
     
    </script>
     
    <div>Currently Logged User:
        <span id="userLogin"></span>
    </div>
    

    MS 参考:https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code

    【讨论】:

    • 我会试试,如果它返回域\帐户这就是我需要的。
    • 我试过了,但不幸的是它没有返回我需要的东西。特别是它不会返回帐户注册到的实际域。可能是因为这是只有 SharePoint 2013 时注册的旧帐户。目前我还没有找到如何从 SharePoint Online 获取 SharePoint 2013 中使用的原始域\用户名的方法。这就是我试图从身份验证提示中获取此信息的原因。
    【解决方案2】:

    如果有人对此仍然感兴趣,我找到了一种使用 microsoft graph API https://docs.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http 获取用户主体名称的方法

    https://graph.microsoft.com/v1.0/me?$select=mailnickname,onpremisesUserprincipalName 是我需要的实际请求

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2020-08-28
    相关资源
    最近更新 更多