我正在尝试修改默认的MVC项目,这样我就可以显示他们的全名,而不是显示用户名。例如,默认应用程序显示
Hello <username>! Log off
我在 ApplicationUser 类中添加了一个新属性 FullName 。显示名称的代码是:
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues:=Nothing, htmlAttributes:=New With {.title = "Manage"})
那么如何从ApplicationUser类中查找该值并在此处显示它?另外,有没有办法缓存这个?对每个请求执行查找似乎都是浪费。
我也可能想要显示他们的电子邮件地址,所以我肯定需要使用新的属性。
答案 0 :(得分:1)
我通常喜欢在登录时序列化FormsAuthentication cookie中的用户对象,然后创建一个继承自IPrincipal的类,以便我的视图可以读取反序列化的对象:
public interface IUserPrincipal : IPrincipal
{
int Id { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
string Username { get; set; }
}
public class UserPrincipal : IUserPrincipal
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public IIdentity Identity { get; private set; }
public UserPrincipal(string Username)
{
this.Identity = new GenericIdentity(Username);
}
}
public class UserPrincipalPoco
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
}
然后在验证时:
public ActionResult Login(LoginViewModel vm, string ReturnUrl)
{
// Check for valid authentication
if (_authenticationService.Authenticate(vm.Username, vm.Password))
{
// Add forms authentication cookie
Response.Cookies.Add(GetFormsAuthenticationCookie(vm.Username));
// Redirect after authentication
}
// Failed authentication, redirect to unauthorized
}
private HttpCookie GetFormsAuthenticationCookie(string Username)
{
var user = _userService.GetUserByUsername(Username);
UserPrincipalPoco pocoModel = new UserPrincipalPoco();
pocoModel.Id = user.Id.Value;
pocoModel.FirstName = user.FirstName;
pocoModel.LastName = user.LastName;
pocoModel.Username = Username;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string userData = serializer.Serialize(pocoModel);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
Username,
DateTime.Now,
DateTime.Now.AddMinutes(15),
false,
userData);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
return new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
}
然后在global.asax.cs:
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
JavaScriptSerializer serializer = new JavaScriptSerializer();
UserPrincipalPoco serializeModel = serializer.Deserialize<UserPrincipalPoco>(authTicket.UserData);
UserPrincipal newUser = new UserPrincipal(authTicket.Name);
newUser.Id = serializeModel.Id;
newUser.FirstName = serializeModel.FirstName;
newUser.LastName = serializeModel.LastName;
newUser.Username = serializeModel.Username;
HttpContext.Current.User = newUser;
}
}
现在您需要创建一个继承自WebViewPage的BaseViewPage,以告诉您的视图使用您的UserPrincipal对象:
public abstract class BaseViewPage : WebViewPage
{
public virtual new UserPrincipal User
{
get { return base.User as UserPrincipal; }
}
}
public abstract class BaseViewPage<TModel> : WebViewPage<TModel>
{
public virtual new UserPrincipal User
{
get { return base.User as UserPrincipal; }
}
}
并在您的Web.config中告诉您的视图始终使用此BaseViewPage:
<pages pageBaseType="MyNameSpace.Views.BaseViewPage">
现在在我的观看中,我可以像以下一样访问用户:
@User.Username
或
@User.FirstName @User.LastName
答案 1 :(得分:-1)
你可以做的几种方法。
如果应用程序不是太大,您可以缓存登录特定时间段的用户模型,以便您可以根据用户名提取整个用户信息。
如果User.Identity中的信息,您可以保存一个列表,包括用户名,名字,姓氏等,用逗号等分隔。
一种糟糕的方式:每当你需要额外信息时,点击数据库并获取它们。
我的观点:缓存已登录特定时间段的最近用户。您将能够使用兑现创建光滑的解决方案。如果您需要信息,请告诉我。