Get the current user, within an ApiController action, without passing the userID as a parameter

前端 未结 8 1383
时光说笑
时光说笑 2020-12-02 09:50

How do we get the current user, within an secure ApiController action, without passing the userName or userId as a parameter?

We assume that this is available, beca

8条回答
  •  既然无缘
    2020-12-02 10:11

    Hint lies in Webapi2 auto generated account controller

    Have this property with getter defined as

    public string UserIdentity
            {
                get
                {
                    var user = UserManager.FindByName(User.Identity.Name);
                    return user;//user.Email
                }
            }
    

    and in order to get UserManager - In WebApi2 -do as Romans (read as AccountController) do

    public ApplicationUserManager UserManager
            {
                get { return HttpContext.Current.GetOwinContext().GetUserManager(); }
            }
    

    This should be compatible in IIS and self host mode

提交回复
热议问题