在with test(id, name, score) as (
select 1,'Alex',10 from dual union all
select 2,'Jim',20 from dual union all
select 3,'Ming',12 from dual union all
select 4,'Alex',15 from dual union all
select 5,'Alex',17 from dual union all
select 6,'Ming',11 from dual
)
select id, name, score
from (
select row_number() over (partition by name order by score desc) as RN,
id, name, score
from test
)
where RN = 1
中,ASP.Net MVC 5
可以扩展为具有自定义属性。我已将其扩展为现在它有一个名为ApplicationUser
的新属性:
DisplayName
我还使用// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser {
public string ConfirmationToken { get; set; }
public string DisplayName { get; set; } //here it is!
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
Update-Database
中的Package-Manager Console
命令更新了数据库表,以确保Visual Studio
ApplicationUser
与{{}之间的一致性1}}表。我已确认名为class
的新列现在存在于AspNetUsers
表中。
现在,我想对原始DisplayName
AspNetUsers
中的文字使用DisplayName
而不是默认UserName
。但正如你所看到的那样:
_LoginPartial.cshtml
原始View
正在使用<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
获取_LoginPartialView.cshtml
的{{1}}。 User.Identity.GetUserName()
有UserName
以及ApplicationUser
,User.Identity
等...但是如何让GetUserId
显示?
答案 0 :(得分:9)
在ClaimsIdentity中添加声明:
public class ApplicationUser : IdentityUser
{
...
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim("DisplayName", DisplayName));
return userIdentity;
}
}
创建了一个扩展方法,用于从ClaimsIdentity中读取DisplayName:
public static class IdentityExtensions
{
public static string GetDisplayName(this IIdentity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var ci = identity as ClaimsIdentity;
if (ci != null)
{
return ci.FindFirstValue("DisplayName");
}
return null;
}
}
在您的视图中使用它:
User.Identity.GetDisplayName()