简体   繁体   English

从控制器获取和编辑当前经过身份验证的用户

[英]Get and edit currently authenticated user from a controller

I am building an ASP.NET MVC application and need to get and edit the current user logged in the application. 我正在构建一个ASP.NET MVC应用程序,需要获取和编辑登录该应用程序的当前用户。 I use the build in IdentityModel and coded a couple of get and edit methods on the AccountController in a similar way as presented here: How to get current user, and how to use User class in MVC5? 我使用IdentityModel中的构建,并以与此处所示类似的方式在AccountController上编码了几个get和edit方法: 如何获取当前用户,以及如何在MVC5中使用User类?

AccountController methods: AccountController方法:

[NonAction]
public async Task<ApplicationUser> getCurrentUser(string userid)
{
    var user = (ApplicationUser)await UserManager.FindByIdAsync(userid);
    return user;
}

[NonAction]
public async Task<int> editCurrentUser(ApplicationUser user)
{
    await UserManager.UpdateAsync(user);
    return 1;
}

My problem is when I try to call them from another controller, in this case to update the balance attribute from the payments controller : 我的问题是,当我尝试从另一个控制器调用它们时,在这种情况下,要从付款控制器中更新balance属性:

var currentUser = await Controllers.AccountController.getCurrentUser(User.Identity.GetUserId());

because AccountController getCurrentUser and editCurrentUser are not static I cannot access them from the class. 因为AccountController getCurrentUser和editCurrentUser不是静态的,所以无法从类中访问它们。 Quite obvious, but where does the MVC application create an instance of AccountController and how do I access it? 很明显,但是MVC应用程序在哪里创建AccountController的实例,如何访问它?

Alternatively, a better approach to retrieve and modify the current user? 或者,是否有更好的方法来检索和修改当前用户?

There are a range of options here. 这里有多种选择。 Perhaps the most straightforward method for you is to make these methods, extension methods: 也许对您来说最直接的方法是使这些方法成为扩展方法:

[NonAction]
public async Task<ApplicationUser> getCurrentUser(string userid)
{
    var user = (ApplicationUser)await UserManager.FindByIdAsync(userid);
    return user;
}

[NonAction]
public async Task<int> editCurrentUser(ApplicationUser user)
{
    await UserManager.UpdateAsync(user);
    return 1;
}

So create a class like: 因此,创建一个像这样的类:

public static class UserManagerExtensions
{
        public static async Task<ApplicationUser> getCurrentUser(this UserManager userManager, string userid)
        {
            var user = (ApplicationUser)await userManager.FindByIdAsync(userid);
            return user;
        }

        public static async Task<int> editCurrentUser(this UserManager userManager, ApplicationUser user)
        {
            await userManager.UpdateAsync(user);
            return 1;
        }
}

And then call those extensions methods from each controller. 然后从每个控制器调用这些扩展方法。 This would mean you inject into PaymentsController, a UserManager etc just like is done in AccountController. 这意味着您将像在AccountController中一样注入到PaymentsController,UserManager等中。

Alternatively you could use ActionFilters, create a common base class for Accounts, Payments controller, or dependency injection. 或者,您可以使用ActionFilters,为Accounts,Payments控制器或依赖项注入创建通用基类。

You shouldn't call getCurrentUser from controller. 您不应从控制器调用getCurrentUser。 Why can't you call (ApplicationUser)await UserManager.FindByIdAsync(userid); 为什么不能调用(ApplicationUser)await UserManager.FindByIdAsync(userid); directly? 直? The better way - put your methods working with users in special class (service) and reference it in placec you need to call this methods. 更好的方法-将与用户一起使用的方法放在特殊的类(服务)中,并在需要调用此方法的场所中引用它。

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

相关问题 如何从HTTP筛选器获取经过身份验证的用户名,IP地址和控制器操作? - How to get authenticated user's name, IP address, and the controller action being called from an HTTP Filter? 如何在使用IdentityServer进行身份验证后获取WebAPI控制器上的用户信息? - How to get user's information on WebAPI controller after authenticated with IdentityServer? 在WCF服务中检索当前已认证的用户 - Retrieving the currently authenticated user in a WCF service 根据当前经过身份验证的用户创建 VssCredentials - Create VssCredentials based on the currently authenticated user 获取Intranet身份验证用户 - Get intranet authenticated user 从.Net Web API中经过Azure身份验证的AD获取用户名 - Get user name from Azure authenticated AD in .Net Web API 如何从.Net Core中的单独类库中获取经过身份验证的用户 - How to get the authenticated user from a separate class library in .Net Core 尝试将对象链接到当前经过身份验证的用户时发生异常 - Exception when attempting to link an object to the currently authenticated user 如何获得最近通过身份验证的用户? - How to get recently authenticated user? 获取经过身份验证的用户的全名 - get the Full name of the authenticated user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM