TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Gordana Tasic
NA
11
703
How to display logged user name in windows form app?
Mar 22 2021 2:52 PM
I'm creating a Windows form application using Graph API. In application, I have more forms. Also, I have a function for logging in the user and when the user logs in his name is written on the label on the first form. On other forms I have a Cancel button, so when the user clicks on the Cancel button, the first form appears, but user name isn't written on the label. Does anyone know how to display user name on the first form when user clicks Cancel button? Here is the code:
public
static
class
GraphHelper
{
private
static
string
[] scopes =
new
string
[] {
"user.read"
};
public
static
string
TokenForUser =
null
;
public
static
DateTimeOffset expiration;
private
const
string
ClientId =
"599ed98d-4356-4a96-ad37-04391e9c48dc"
;
private
const
string
Tenant =
"common"
;
private
const
string
Authority =
"https://login.microsoftonline.com/"
+ Tenant;
// The MSAL Public client app
private
static
IPublicClientApplication PublicClientApp;
private
static
string
MSGraphURL =
"https://graph.microsoft.com/beta/"
;
private
static
AuthenticationResult authResult;
public
static
GraphServiceClient graphClient;
public
static
string
token;
public
static
GraphServiceClient GetGraphClient(
string
token)
{
if
(graphClient ==
null
)
{
// Create Microsoft Graph client.
try
{
graphClient =
new
GraphServiceClient(
"https://graph.microsoft.com/beta"
,
new
DelegateAuthenticationProvider(
async (requestMessage) =>
{
requestMessage.Headers.Authorization =
new
AuthenticationHeaderValue(
"bearer"
, token);
// This header has been added to identify our sample in the Microsoft Graph service. If extracting this code for your project please remove.
requestMessage.Headers.Add(
"SampleID"
,
"uwp-csharp-snippets-sample"
);
}));
return
graphClient;
}
catch
(Exception ex)
{
Debug.WriteLine(
"Could not create a graph client: "
+ ex.Message);
}
}
return
graphClient;
}
public
static
async Task<
string
> GetTokenForUserAsync()
{
if
(TokenForUser ==
null
|| expiration <= DateTimeOffset.UtcNow.AddMinutes(10))
{
PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
.WithAuthority(Authority)
.WithRedirectUri(
"https://login.microsoftonline.com/common/oauth2/nativeclient"
)
.WithLogging((level, message, containsPii) =>
{
Debug.WriteLine($
"MSAL: {level} {message} "
);
}, LogLevel.Warning, enablePiiLogging:
false
, enableDefaultPlatformLogging:
true
)
.Build();
// It's good practice to not do work on the UI thread, so use ConfigureAwait(false) whenever possible.
IEnumerable<IAccount> accounts = await PublicClientApp.GetAccountsAsync().ConfigureAwait(
false
);
IAccount firstAccount = accounts.FirstOrDefault();
try
{
authResult = await PublicClientApp.AcquireTokenSilent(scopes, firstAccount)
.ExecuteAsync();
}
catch
(MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
Debug.WriteLine($
"MsalUiRequiredException: {ex.Message}"
);
authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
.ExecuteAsync()
.ConfigureAwait(
false
);
}
TokenForUser = authResult.AccessToken;
}
return
TokenForUser;
}
public
static
async Task<User> GetMeAsync(
string
token)
{
graphClient = GetGraphClient(token);
try
{
// GET /me
return
await graphClient.Me
.Request()
.Select(u =>
new
{
u.DisplayName
})
.GetAsync();
}
catch
(ServiceException ex)
{
return
null
;
}
}
}
public
partial
class
Form1 : Form
{
public
static
string
token;
public
static
GraphServiceClient graphClient;
public
Form1()
{
InitializeComponent();
}
private
async
void
button1_Click(
object
sender, EventArgs e)
{
token = await GraphHelper.GetTokenForUserAsync();
User graphUser = await GraphHelper.GetMeAsync(token);
label4.Text = graphUser.DisplayName;
}
}
public
partial
class
Form2 : Form
{
public
Form2()
{
InitializeComponent();
}
private
void
button2_Click(
object
sender, EventArgs e)
{
Form1 f1 =
new
Form1();
this
.Close();
f1.Show();
}
}
Reply
Answers (
2
)
application working fine in all browsers except IE
Send Email Using SendGrid