I have SharePoint Online classic page which has some custom JavaScript and jQuery scripts implemented and third party .NET app requiring some basic auth. 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.
_spPageContextInfo.userId
Another suggestion is to use the 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
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
https://graph.microsoft.com/v1.0/me?$select=mailnickname,onpremisesUserprincipalName was the actual request I needed
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.