This repository was archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathHomeController.cs
More file actions
101 lines (85 loc) · 3.3 KB
/
HomeController.cs
File metadata and controls
101 lines (85 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the source repository root for complete license information.
*/
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Graph;
using Microsoft.AspNetCore.Hosting;
using System.Security.Claims;
using MicrosoftGraphAspNetCoreConnectSample.Services;
namespace MicrosoftGraphAspNetCoreConnectSample.Controllers
{
public class HomeController : Controller
{
private readonly IWebHostEnvironment _env;
private readonly IGraphServiceClientFactory _graphServiceClientFactory;
public HomeController(IWebHostEnvironment hostingEnvironment, IGraphServiceClientFactory graphServiceClientFactory)
{
_env = hostingEnvironment;
_graphServiceClientFactory = graphServiceClientFactory;
}
[AllowAnonymous]
// Load user's profile.
public async Task<IActionResult> Index(string email)
{
if (User.Identity.IsAuthenticated)
{
// Get users's email.
email ??= User.FindFirst("preferred_username")?.Value;
ViewData["Email"] = email;
// Initialize the GraphServiceClient.
var graphClient = _graphServiceClientFactory.GetAuthenticatedGraphClient((ClaimsIdentity)User.Identity);
ViewData["Response"] = await GraphService.GetUserJson(graphClient, email, HttpContext);
ViewData["Picture"] = await GraphService.GetPictureBase64(graphClient, email, HttpContext);
}
return View();
}
[Authorize]
[HttpPost]
// Send an email message from the current user.
public async Task<IActionResult> SendEmail(string recipients)
{
if (string.IsNullOrEmpty(recipients))
{
TempData["Message"] = "Please add a valid email address to the recipients list!";
return RedirectToAction("Index");
}
try
{
// Initialize the GraphServiceClient.
var graphClient = _graphServiceClientFactory.GetAuthenticatedGraphClient((ClaimsIdentity)User.Identity);
// Send the email.
await GraphService.SendEmail(graphClient, _env, recipients, HttpContext);
// Reset the current user's email address and the status to display when the page reloads.
TempData["Message"] = "Success! Your mail was sent.";
return RedirectToAction("Index");
}
catch (ServiceException se)
{
if (se.Error.Code == "Caller needs to authenticate.") return new EmptyResult();
return RedirectToAction("Error", "Home", new { message = "Error: " + se.Error.Message });
}
}
[AllowAnonymous]
public IActionResult About()
{
return View();
}
[AllowAnonymous]
public IActionResult Contact()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[AllowAnonymous]
public IActionResult Error()
{
return View();
}
}
}