Hi, How to validate user using Office 365 in asp.net core web api 8. or how can we use SSO using office 365 in asp.net core web api 8 .
If any one have any idea. Please share me
Regards,
Amit
Hi, How to validate user using Office 365 in asp.net core web api 8. or how can we use SSO using office 365 in asp.net core web api 8 .
If any one have any idea. Please share me
Regards,
Amit
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sahil Kataria150 reputationPosted Feb 27, 2024, 6:48 AM EST
To validate a user using Office 365 in an ASP.NET Core Web API, you can utilize Microsoft's Azure Active Directory (Azure AD) for authentication and authorization. Here's a basic outline of how you can achieve this:
Register your application with Azure AD: Go to the Azure portal and register your application. This will give you a client ID and client secret that your ASP.NET Core Web API can use to authenticate with Azure AD.
Startup.csfile, configure authentication services to use Azure AD. You will need to install theMicrosoft.AspNetCore.Authentication.AzureAD.UINuGet package if you haven't already.services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
3.Set up authorization: Define authorization policies in your
Startup.csfile to secure your API endpoints.To validate a user using Office 365 in an ASP.NET Core Web API, you can utilize Microsoft's Azure Active Directory (Azure AD) for authentication and authorization. Here's a basic outline of how you can achieve this:
Register your application with Azure AD: Go to the Azure portal and register your application. This will give you a client ID and client secret that your ASP.NET Core Web API can use to authenticate with Azure AD.
Configure authentication in your ASP.NET Core Web API: In your
Startup.csfile, configure authentication services to use Azure AD. You will need to install theMicrosoft.AspNetCore.Authentication.AzureAD.UINuGet package if you haven't already.csharpCopy code
services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme) .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));Set up authorization: Define authorization policies in your
Startup.csfile to secure your API endpoints.csharpCopy code
services.AddAuthorization(options => { options.AddPolicy("AzureADPolicy", policy => policy.Requirements.Add(new Microsoft.Identity.Web.Requirements.AzureAdAuthorizationRequirement())); });Add authorization attribute to your controllers or endpoints: Decorate your API controllers or individual endpoints with the
[Authorize(Policy = "AzureADPolicy")]attribute.csharpCopy code
[Authorize(Policy = "AzureADPolicy")] [ApiController] [Route("api/[controller]")] public class YourController : ControllerBase { // Controller code here }Handle authentication and authorization in your controllers: Once the user is authenticated and authorized, you can access user information from the
HttpContext.Userobject.csharpCopy code
[HttpGet] public IActionResult Get() { var user = HttpContext.User; // Access user information var userName = user.Identity.Name; var userEmail = user.Claims.FirstOrDefault(c => c.Type == "email")?.Value; // Your logic here return Ok(); }Testing: You can test your API by obtaining an access token from Azure AD using OAuth 2.0 authentication. You can use tools like Postman or write a client application to obtain the token and make requests to your API.
By following these steps, your ASP.NET Core Web API will be protected by Azure AD authentication, allowing you to validate users using Office 365 credentials. Make sure to handle exceptions and errors appropriately, and consider implementing logging and monitoring for better visibility into your authentication process.
Sahil Kataria150 reputationPosted Mar 19, 2024, 9:54 AM EDT
https://learn.microsoft.com/en-us/office/dev/add-ins/develop/sso-in-office-add-ins