0

My view's code[_LoginPartial.cshtml] is like below. I want to show more details like department. How to do it? I succeed in getting departments in IdentityModels.cs. But I don't know how to use it in view.

IdentityModels.cs

   var directoryEntry = new System.DirectoryServices.DirectoryEntry();
    var directorySearcher = new System.DirectoryServices.DirectorySearcher(directoryEntry); 

    directorySearcher.Filter = string.Format("(&(objectClass=user)(SamAccountName={0}))", mADUser.SamAccountName);

    var result = directorySearcher.FindOne();


    var entry = result.GetDirectoryEntry();

var (string)mADUserDirectoryEntry.Properties["department"].Value;

_LoginPartial.cshtml

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Login", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
        @Html.AntiForgeryToken()

        <ul class="nav navbar-nav navbar-right">
            <li>
                @Html.ActionLink("Hi" + User.Identity.GetUserName(), "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage", @style = "color: white" })
                <a href="javascript:document.getElementById('logoutForm').submit()">ログオフ</a>
            </li>
            <li></li>
        </ul>

    }
}
Jeff_hu
  • 417
  • 4
  • 15
  • The link below solved my problem. https://stackoverflow.com/questions/28335353/how-to-extend-available-properties-of-user-identity – Jeff_hu Aug 19 '19 at 09:26

1 Answers1

0

Since your View Model is of type Microsoft.AspNet.Identity you only have properties available that comes out of the box. Unless you extend the class.

Here is how you can do that: How to extend available properties of User.Identity Then you will be able to use the department data as User.Identity.Department or User.Identity.GetDepartment().

Rahatur
  • 3,147
  • 3
  • 33
  • 49