简体   繁体   English

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

[英]How to get the logged user with an OWIN authentication?

The login code in my AccountController我的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);

In my PrivateController I want to access to the Id of the authenticated user, I'm searching an ID field or something like that but在我的PrivateController 中,我想访问经过身份验证的用户的 ID,我正在搜索 ID 字段或类似的内容,但是

在此处输入图片说明

How to get the logged user with an OWIN authentication?如何使用 OWIN 身份验证获取登录用户?

Edit编辑

Request.GetOwinContext().Authentication.User.Identity

Gives me access to the following propertys:使我可以访问以下属性:

  • AuthenticationType认证类型
  • IsAuthenticated已认证
  • Name //Claim.Name名称 //Claim.Name

How can I set an ID like a 'Claim.ID' and retrieve it?如何设置像“Claim.ID”这样的 ID 并检索它?

Second edit第二次编辑

This post https://stackoverflow.com/a/24893167/4470880 solved my problem.这篇文章https://stackoverflow.com/a/24893167/4470880解决了我的问题。

From the Microsoft.AspNet.Identity.Core NuGet package https://www.nuget.org/packages/Microsoft.AspNet.Identity.Core/ , in the Microsoft.AspNet.Identity namespace, there are extension methods to get the user's ID and Name -- see MSDN IdentityExtensions Class (Microsoft.AspNet.Identity)Microsoft.AspNet.Identity.Core NuGet 包https://www.nuget.org/packages/Microsoft.AspNet.Identity.Core/ ,在Microsoft.AspNet.Identity命名空间中,有获取用户 ID 的扩展方法和名称——请参阅MSDN IdentityExtensions 类 (Microsoft.AspNet.Identity)

usage is like this (from a controller):用法是这样的(来自控制器):

string userId = this.HttpContext.User.Identity.GetUserId();

You can also write something like that :你也可以这样写:

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using System.Web;
using System.Web.Mvc;

public class BaseController : Controller
{
    protected ApplicationUser CurrentUser
    {
        get { return System.Web.HttpContext.Current.GetOwinContext()
                    .GetUserManager<ApplicationUserManager>()
                    .FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); 
            }
    }

    //...
}

So you write a BaseController which inherits from Controller .所以你写了一个继承自ControllerBaseController
Then, all your controllers will heritate from BaseController ;然后,您的所有控制器都将从BaseController so you could use CurrentUser.Id in all your controllers所以你可以在所有控制器中使用CurrentUser.Id

Try this alternative:试试这个替代方案:

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


For getting name of the user :获取user

string name = HttpContext.GetOwinContext().Authentication.User.Identity.Name;

Hope this helps...希望这可以帮助...

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

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