简体   繁体   English

如何从身份验证提示弹出窗口中获取用户名

[英]How to get username from authentication prompt popup

I have SharePoint Online classic page which has some custom JavaScript and jQuery scripts implemented and third party .NET app requiring some basic auth.我有 SharePoint 在线经典页面,其中有一些自定义 JavaScript 和 jQuery 脚本已实现,第三方 Z303CB0EF5825825 应用程序需要 BBBE0EF9EDB90892AZD61。 When the user visits the page he is prompted to enter username and password.当用户访问该页面时,系统会提示他输入用户名和密码。 How can I get the username from this prompt?如何从此提示中获取用户名? Prompt is standard and looks like this (picture is not mine)提示是标准的,看起来像这样(图片不是我的) 在此处输入图像描述

You can use SharePoint's javascript API to retrieve the current user's information.您可以使用 SharePoint 的 javascript API 来检索当前用户的信息。

_spPageContextInfo.userId

see: https://social.technet.microsoft.com/wiki/contents/articles/29766.sharepoint-understanding-the-sppagecontextinfo-object.aspx请参阅: https://social.technet.microsoft.com/wiki/contents/articles/29766.sharepoint-understanding-the-sppagecontextinfo-object.aspx

Another suggestion is to use the API:另一个建议是使用 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 reference: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code微软参考: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code

If someone is still interested in this I found a way to get the user principal name with microsoft graph API https://docs.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http如果有人仍然对此感兴趣,我找到了一种使用 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 was the actual request I needed https://graph.microsoft.com/v1.0/me?$select=mailnickname,onpremisesUserprincipalName是我需要的实际请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM