c# - 如何使用 OWIN 身份验证获取登录用户?

标签 c# asp.net-mvc owin

我的AccountController中的登录码

var claims = new List<Claim>();  
claims.Add(new Claim(ClaimTypes.Name, user.Name+" "+user.Surname));
claims.Add(new Claim(ClaimTypes.Role, role.Code));
var claimIdenties = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, claimIdenties);

在我的 PrivateController 中,我想访问经过身份验证的用户的 ID,我正在搜索 ID 字段或类似的东西,但是

enter image description here

如何通过 OWIN 身份验证获取登录用户?

编辑

Request.GetOwinContext().Authentication.User.Identity

使我能够访问以下属性:

  • 身份验证类型
  • 已通过身份验证
  • 姓名//Claim.Name

如何设置像“Claim.ID”这样的 ID 并检索它?

第二次编辑

此帖https://stackoverflow.com/a/24893167/4470880解决了我的问题。

最佳答案

试试这个替代方案:

var user = HttpContext.GetOwinContext().Authentication.User;

获取“用户”的名称:
string name = HttpContext.GetOwinContext().Authentication.User.Identity.Name;

关于c# - 如何使用 OWIN 身份验证获取登录用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50946339/

相关文章:

javascript - 如何使用 ASP.NET MVC 索引 View 分页中的页面大小下拉菜单?

asp.net-mvc - MVC 5 OWIN 身份验证(谷歌范围)/没有 EF 的身份

c# - 欧文阶段标记

c# - 在不安装 Lotus Notes 客户端的情况下访问 asp.net/C3 中的 Domino 邮件

c# - 如何在保存到 SQL 数据库并检索它以显示在标签内时保持换行符

c# - 当结果可能时,IQueryable ToList() 是否优于 .Count()?

c# - 如何使用 facebook api 在 Facebook 请求的消息字段中添加 URL?

c# - 当前上下文中不存在名称 'loginrand'

c# - MVC 内容文件夹问题 - 未找到 Twitter Bootstrap 样式表

ios - ASP.NET 网络 API 2 : Login with external provider via native mobile (iOS) app