简体   繁体   English

C#Web API 2和Angular-Microsoft帐户身份验证

[英]C# Web API 2 & Angular - Microsoft Account Authentication

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 Authentication Get logged user from controller C#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. 我想做的是创建一个具有C#Web API 2后端和Angular 2前端的应用程序。 Now, I want that my authentication be using people's Microsoft Account which means this will be an external authentication. 现在,我希望我的身份验证使用人们的Microsoft帐户,这意味着这将是一个外部身份验证。

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. 在上面的链接中,我使用了msal.js,到目前为止,它对我来说还算不错,直到我不得不获取登录用户的详细信息为止。 It was possible from Angular's side but I want to do it in Web API so it is more secured. 从Angular的角度来看这是可能的,但是我想在Web API中做到这一点,因此它更加安全。

Thanks in advance! 提前致谢!

From your code in the previous post, it looks like you need to read from the ClaimsPrincipal instead. 从上一篇文章中的代码中,您似乎需要从ClaimsPrincipal中读取内容。 ClaimsPrincipal is the implementation of IPrincipal when you use OAuthBearerTokens, so of course you can get the username from CurrentPrincipal.Current.Identity 使用OAuthBearerTokens时ClaimsPrincipal是IPrincipal的实现,因此当然可以从CurrentPrincipal.Current.Identity获取用户名

From this documentation 从本文档

https://msdn.microsoft.com/en-us/library/system.security.claims.claimsprincipal(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.security.claims.claimsprincipal(v=vs.110).aspx

https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-api-dotnet https://docs.microsoft.com/zh-cn/azure/active-directory-b2c/active-directory-b2c-devquickstarts-api-dotnet

public IEnumerable<Models.Task> Get()
{
    var user = ClaimsPrincipal.Current;
    ...
}

If you are using OpenId, you have claims that are returned when user is authorized. 如果您使用的是OpenId,则您将拥有在授权用户时返回的声明。 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. 我假设您正在使用Azure B2C进行授权,在这种情况下,您可以选择将作为令牌的一部分返回的蛤。

For example, if you want to fetch user id: 例如,如果要获取用户标识:

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

Email: 电子邮件:

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. 并检查它,它应该返回索赔清单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM