close

前言:

應用ASP.NET MVC (Authentication and Authorization in ASP.NET Web API) 做註冊,加密提升了安全性

但是登入後的畫面讓我不甚滿意! 

上網查之後才達到自己要的功能

解決方式:

Step1: 創建新的class,但最重要的是namespace要寫好

 public static class IdentityExtensions
    {
        public static string Get員工名稱(this IIdentity identity)
        {
            var claim = ((ClaimsIdentity)identity).FindFirst("員工名稱");
            // Test for null to avoid issues during local testing
            return (claim != null) ? claim.Value : string.Empty;
        }
    }

Step2: 新增自訂使用者宣告

 GenerateUserIdentityAsync方法中添加 userIdentity.AddClaim(new Claim("員工名稱", this.員工名稱.ToString()));

記得創建 public string 員工名稱 { get; set; }

Step3: 

  @Html.ActionLink("Hello " + User.Identity.Get員工名稱() + "!!!!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" ,style= "top:-10px;color:chartreuse" })
              

這樣就完成了~

參考資料

http://stackoverflow.com/questions/28335353/how-to-extend-available-properties-of-user-identity

arrow
arrow
    全站熱搜

    程式小試身手 發表在 痞客邦 留言(0) 人氣()