【问题标题】:MVC5 - Most Efficient Way to Access the User ObjectMVC5 - 访问用户对象的最有效方式
【发布时间】:2013-09-11 20:19:15
【问题描述】:

在学习 C# 和 MVC 时,我在 VS2013 Preview 中使用股票 MVC 模板,我试图获取对当前登录的两个用户类(模板在 IdentityModel.cs 文件中创建的内容)的引用在用户中(如果适用)或来自用户 id guid。

目前我有这个(并且它有效),但它似乎有点复杂并且可能很昂贵(我不确定它是如何运作的)。

IUser iUser = await Users.Find(User.Identity.GetUserId());  //have to reference Microsoft.AspNet.Identity to access the GetUserId method
IUser iUserFromId = await Users.Find("user id guid placeholder");

User user = iUser != null ? (User)iUser : null;

有没有更清洁或更有效的方法来做到这一点?没有异步方法是否可以做到这一点?

【问题讨论】:

    标签: asp.net asp.net-mvc visual-studio-2013 asp.net-mvc-5 asp.net-identity


    【解决方案1】:

    新的身份 api 旨在异步,因此您可以正确执行。

    你可以在一行中写成:

    User user = await Users.Find(User.Identity.GetUserId()) as User;
    

    在 RTM 中它会发生一些变化,但它的要点是相同的,除了 Manager 将具有通用性,因此您不必进行演员表并且看起来更像:

    User user = await Users.FindAsync(User.Identity.GetUserId())
    

    【讨论】:

    • RTM 代表什么?
    猜你喜欢
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 2017-03-08
    • 2017-03-09
    • 1970-01-01
    相关资源
    最近更新 更多