首页 > 编程语言 >How to get User Name from External Login in ASP.NET Core?

How to get User Name from External Login in ASP.NET Core?

时间:2023-08-07 16:46:08浏览次数:54  
标签:info Core ASP Name ClaimTypes Principal public signInManager string

How to get User Name from External Login in ASP.NET Core?

 

回答1

Do you have access to SignInManager or can you inject it? If yes, then this is how you would access user id (username), email, first & last name:

public class MyController : Microsoft.AspNetCore.Mvc.Controller
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;

    public MyController (
        UserManager<ApplicationUser> userManager,
        SignInManager<ApplicationUser> signInManager
    )
    {
        _userManager = userManager;
        _signInManager = signInManager;        
    }

    public async Task<IActionResult> MyAction(){
    ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync();
    string userId = info.Principal.GetUserId()
    string email = info.Principal.FindFirstValue(ClaimTypes.Email);
    string FirstName = info.Principal.FindFirstValue(ClaimTypes.GivenName) ?? info.Principal.FindFirstValue(ClaimTypes.Name);
    string LastName = info.Principal.FindFirstValue(ClaimTypes.Surname);
    }
}

GetUserId extension:

public static class ClaimsPrincipalExtensions
{
    public static string GetUserId(this ClaimsPrincipal principal)
    {
        if (principal == null)
            return null; //throw new ArgumentNullException(nameof(principal));

        string ret = "";

        try
        {
            ret = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
        }
        catch (System.Exception)
        {                
        }
        return ret;
    }
}

 

标签:info,Core,ASP,Name,ClaimTypes,Principal,public,signInManager,string
From: https://www.cnblogs.com/chucklu/p/17611814.html

相关文章

  • Coreference Resolution 对于OntoNotes 5.0数据集的预处理操作
    1.下载数据集1.1下载Conll-2012相关数据集和脚本1.2下载OntoNotes......
  • kubernetes-发布netcore项目
    一,安装k8s环境 参考:https://www.yuque.com/fairy-era/yg511q/lmy7gc二,通过Dockerfile文件将本地项目打包成镜像。发送到本地仓库或者阿里云(https://promotion.aliyun.com/ntms/act/kubernetes.html),方便在node机器上拉取镜像,不然每个node机器都要重新制作镜像。三,在k8s的ma......
  • .NET Core框架、库和软件的中文收录大全
    .NETCore框架、库和软件的中文收录大全。内容包括:库、工具、框架、模板引擎、身份认证、数据库、ORM框架、图片处理、文本处理、机器学习、日志、代码分析、教程等。这里记录的大部分可以链接到github上,Nuget上也有对应的包,这里只记录比较牛的项目。目录微软ORMIOC日志......
  • vue报错 Multiple assets emit different content to the same filename index.html
    vue-cli版本:@vue/[email protected]报错现象:想把css和script全部内嵌到html文件中,就用了"HtmlInlineScriptPlugin"插件,打包后js代码被嵌到了head里,导致代码提前执行找不到#app,再配置HtmlWebpackPlugin插件通过inject:"body"指定代码内嵌到body,打包报错"Multipleassetsemitdiff......
  • 从浅入深了解.NET Core MVC 2.x全面教程
    一、基础1.默认配置使用KestrelWebServerASP.NETCore内置——跨平台IIS集成UseIIS()UseIISIntergration()LogIConfiguration接口2.IConfiguration配置信息的来源appsettings.jsonUserSerets环境变量命令行参数XML...3.管道4.MVC5.路由Routin......
  • k8s 部分节点 nodelocaldns [ERROR] Failed to read node-cache coreFile /etc/coredn
      部分K8S节点nodelocaldnsCrashLoopBackOff状态报错,报错信息如下:#kubectllogsnodelocaldns-w9mgz-nkube-system2023/08/0703:18:33[INFO]UsingCorefile/etc/coredns/Corefile2023/08/0703:18:33[ERROR]Failedtoreadnode-cachecoreFile/etc/coredns/Co......
  • 【HMS Core】推送报错907135701、分析数据查看
    ​【关键字】HMS、推送服务、分析服务 【问题描述1】集成推送服务,获取Token时报错907135701:scopelistempty 【解决方案】907135701OpenGW没有配置Scope1、您可以检查下网络是否有问题,手机是否可以正常连接互联网2、查看推送服务开关是否正常打开​​ 【问题......
  • linux(centos版本)下docker部署.netcore项目
    准备:1.要在Linux系统上使用Docker部署系统,你需要安装Docker引擎以及一些其他相关的软件。以下是安装步骤:安装Docker引擎:对于Ubuntu系统,可以使用以下命令安装Docker:sudoapt-getupdatesudoapt-getinstalldocker-cedocker-ce-clicontainerd.......
  • NETCORE - enum 枚举
    NETCORE-enum枚举                 引用:https://www.coder.work/article/969924引用:https://stackoverflow.com/questions/23794691/extension-method-to-get-the-values-of-any-enum ......
  • 构建跨平台的.NET Core控制台应用程序
    在本博客中,我们将介绍如何构建一个跨平台的.NETCore控制台应用程序。.NETCore是一个开源的跨平台框架,它可以在Windows、macOS和Linux等操作系统上运行。通过.NETCore,我们可以轻松地构建具有高度可移植性的应用程序。以下是步骤:步骤1:安装.NETCoreSDK首先,你需要安装.NETCoreSD......