C# ASP.NET标识-扩展_LoginPartial
我发现这很难总结,因此这个问题的名字很糟糕。在使用Identity的.net web应用程序上,有一个自动生成的登录部分,显示用户的用户名属性,如下所示:C# ASP.NET标识-扩展_LoginPartial,c#,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Identity,我发现这很难总结,因此这个问题的名字很糟糕。在使用Identity的.net web应用程序上,有一个自动生成的登录部分,显示用户的用户名属性,如下所示: @Html.ActionLink(User.Identity.GetUserName(), "Index", "Manage", routeValues: null, htmlAttributes: new {title = "Manage"}) 在分析保存用户数据的SQL数据库之后,我注意到对GetUserName()的调用实际上并没有调
@Html.ActionLink(User.Identity.GetUserName(), "Index", "Manage", routeValues: null, htmlAttributes: new {title = "Manage"})
在分析保存用户数据的SQL数据库之后,我注意到对GetUserName()的调用实际上并没有调用数据库来检索名称。我希望访问“ApplicationUser”类的导航属性,该属性包含要显示的缩略图的url
然而,我希望我可以实现这一点,而不需要在每个页面请求上调用数据库来获取URL。这方面的一个例子就是在这个网站上,您的个人资料图像作为布局的一部分显示在每个页面上
应用程序用户类具有此属性
public class ApplicationUser : IdentityUser
{
public UserProfile Profile { get; set; }
}
而user profile类持有此属性
public class UserProfile
{
[Key, ForeignKey("User")]
public string UserId { get; set; }
public ApplicationUser User { get; set; }
[Required]
public string ThumbnailUrl { get; set; }
...
}
我该怎么做?还是有更好的方法来实现这一点 你说得对,它不打电话。。。原因是它将数据保存在客户端的cookie中。。。如果不这样做,服务器就会崩溃,如果它不得不继续来回运行,为大规模系统获取如此琐碎的数据。。。所以它被缓存了 在ASP.NET MVC中实现的方法是通过使用IPrinciple接口。。。我会给你指出正确的方向,让你了解更多。。。
最好创建第二个表(或在当前表中添加列),在注册表中添加文件上载功能。注册成功后,使用EntityFramework将图片添加到数据库中。创建一个页面,通过用户ID返回图片,以便您可以将其包含在某个页面中 你可以用这个主意
您可以将它们添加为索赔。 进行身份验证时,应从数据库中获取用户,并添加以后需要使用的所有属性 假设您在登录用户时使用CreateIdentity,则应返回ClaimSideEntity对象
var userIdentity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
userIdentity.AddClaim(new Claim("Thumbnail", user.ThumbnailUrl));
然后您需要创建一个扩展类,比如IdentityExtensions:
public static class IdentityExtensions{
public static string Thumbnail(this IIdentity identity){
try{
return ((ClaimsIdentity) identity).FindFirst("Thumbnail").Value;
}
catch(Exception ex){
// handle any exception the way you need
}
}
}
最后,在视图中,您应该能够使用@User.Identity.thumboil()不要这样做。。。你不应该这样做的原因是@Asad Ali因为你这样做会使数据库急剧膨胀。。。如果您需要显示图片,请将其上载到一个目录,并提供该目录中的图像。您还没有读取OP,OP正在尝试缓存数据客户端side@Asad阿里:我希望将图像存储在文件系统中,而不是数据库中,因为膨胀的问题和其他一些原因。我知道我们不应该在某些情况下这样做,但在某些情况下是有效的——有不同的观点。但是我给了你一个链接,来检查你如何定制配置文件,而不是如何将图像存储在数据库中。以声明的形式添加属性是处理这个问题的好方法吗?类似于这篇文章的指导: