C#Web API 2和Angular-Microsoft帐户身份验证 [英] C# Web API 2 & Angular - Microsoft Account Authentication

查看:81
本文介绍了C#Web API 2和Angular-Microsoft帐户身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周前,我已经问过以下问题,但没有得到有效的答案.或者也许不适合我的情况.

I've asked the question below a couple weeks ago and I didn't get a working answer. Or maybe just not suitable for my case.

C#Microsoft身份验证获取记录控制器中的用户

所以我想也许我不是在问正确的问题.我想做的是创建一个具有C#Web API 2后端和Angular 2前端的应用程序.现在,我希望我的身份验证使用人们的Microsoft帐户,这意味着这将是一个外部身份验证.

So I thought maybe I wasn't asking the right question. What i'm trying to do is create an app which has a C# Web API 2 backend and an Angular 2 frontend. Now, I want that my authentication be using people's Microsoft Account which means this will be an external authentication.

做到这一点的最佳方法是什么?如果您可以在博客或文章上提供一个链接来解释我的期望,将不胜感激.在上面的链接中,我使用了msal.js,到目前为止,它对我来说还算不错,直到我不得不获取登录用户的详细信息为止.从Angular的角度来看是有可能的,但是我想在Web API中做到这一点,因此它更加安全.

What's the best way of doing this? It would be very much appreciated if you can give a link on a blog or article that explain what I'm looking for. On my link above I've used msal.js and so far it was working fine for me until I had to get the logged user's details. It was possible from Angular's side but I want to do it in Web API so it is more secured.

提前谢谢!

推荐答案

如果您使用的是OpenId,则您拥有在授权用户时会返回的声明.我假设您正在使用Azure B2C进行授权,在这种情况下,您可以选择将作为标记的一部分返回的蛤.

If you are using OpenId, you have claims that are returned when user is authorized. I am assuming you are using Azure B2C for authorization in which case you can select clams that will be returned as part of token.

例如,如果要获取用户ID:

For example, if you want to fetch user id:

var userId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;

电子邮件:

string userName = ClaimsPrincipal.Current.Claims.Where(x => x.Type == "emails").FirstOrDefault()?.Value;

这取决于您的授权已返回的声明,最简单的方法是将断点置于

It depends what claims your authorization has returned, easiest way would be to put breakpoint on

ClaimsPrincipal.Current

并检查它,它应该返回索赔清单.

and inspect it, it should return list of claims.

这篇关于C#Web API 2和Angular-Microsoft帐户身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆