نمایش نام کاربر به جای ایمیل آن پس از ورود به سایت

8/28/2017 MVC
1678

معمولا در پروژه های MVC بعد از ثبت نا م کاربر ایمیل آن در بالا صفحه نمایش داده میشود ، اگر بخواهیم نام کاربر نمایش داده شود چه کنیم

ابتدا  کلاس IdentityModels در پوشه MOdel  را به این صورت تغییر میدهیم

 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
            
            return userIdentity;
        }

        public string FName { get; set; }
        public string LName { get; set; }
        public int? CountryId { get; set; }
        public virtual Country Country { get; set; }
        public int? ProvinceId { get; set; }
        public virtual Province Province { get; set; }
        public int? CityId { get; set; }
        public virtual City City { get; set; }
        public string Address { get; set; }
        public string PostalCode { get; set; }
        public string Mobile { get; set; }
        public string Phone { get; set; }

    }

 

در کد بالا یک سری مشخصات از قبیل آدرس  و .... را به ثبت نام  اربر اضافه کردیم

بعد یک پوشه اضافه کردم و کلاس زیر را اضافه کنیم

using System.Security.Claims;
using System.Security.Principal;
namespace muyProject.Models.Extensions
{
    public static class IdentityExtensions
    {
        public static string GetUserFirstname(this IIdentity identity)
        {
            var claim = ((ClaimsIdentity)identity).FindFirst("FirstName");
            // Test for null to avoid issues during local testing
            return (claim != null) ? claim.Value : string.Empty;
        }
    }


}

لطفا در مورد static  کلاس دقت بفرمایید

سپس کد را به کلاس IdentityModels اضافه میکنیم

 

 

            // Add custom user claims here
            userIdentity.AddClaim(new Claim("FirstName", this.FName));

و کلاس IdentityModels به این صورت خواهد بود

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("FirstName", this.FName));
            return userIdentity;
        }

        public string FName { get; set; }
        public string LName { get; set; }
        public int? CountryId { get; set; }
        public virtual Country Country { get; set; }
        public int? ProvinceId { get; set; }
        public virtual Province Province { get; set; }
        public int? CityId { get; set; }
        public virtual City City { get; set; }
        public string Address { get; set; }
        public string PostalCode { get; set; }
        public string Mobile { get; set; }
        public string Phone { get; set; }

    }

حال برای نمایش نام کاربر در صورتی که نام کاربری را پر کرده باشد میتوانید در _LoginPartial.cshtml را به این صورت تغییر دهید

 

 <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("خوش آمدید " + User.Identity.GetUserFirstname()+ "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">خروج</a></li>
    </ul>