0

I have added DepartmentId column as Foreign Key in AspNetUsers table and also i have the same DepartmentId as ForeignKey in another table called MaterialRequest i want login user's Department Name to be displayed in the View As 'Department Name'

My Classes

Department

DepartmentId DepartmentName

MaterialRequest

MaterialRequestId MaterialName DepartmentId

What i have tried

        public ActionResult Index(int? id)
        {
  var user = User.Identity.GetUserId();
            ViewBag.DeptName=db.Query<AspNetUser>("Select DepartmentId from AspNetUsers where Id = '@0'",id);
}

1 Answer 1

1

There is a way around this, you can create a claim and get this way:

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
    // Add custom user claims here => this.DepartmentId is a value stored in database against the user
    userIdentity.AddClaim(new Claim("DepartmentId", this.DepartmentId.ToString()));

        return userIdentity;
    }

    // Your Extended Properties
    public int? DepartmentId { get; set; }
}

Then create an extension method to get your departmentId:

namespace App.Extensions
{
    public static class IdentityExtensions
    {
        public static string GetDepartmentId(this IIdentity identity)
        {
            var claim = ((ClaimsIdentity)identity).FindFirst("DepartmentId");
            // Test for null to avoid issues during local testing
            return (claim != null) ? claim.Value : string.Empty;
        }
    }
}

And now you can easily call this method to get your DepartmentId:

var departmentId= User.Identity.GetDepartmentId();
Sign up to request clarification or add additional context in comments.

7 Comments

ok but how can i show the departmentname in which user is login
@kunals since you are passing your Identity you don't have to pass the userId to this. basically whomever is logged in will get their own DepartmentId. works like User.Identity.GetUserId()
var departmentId= User.Identity.GetDepartmentId(); with this we are getting the departmentid which the logged in user belongs but how to display that departmentname to my view
@kunals easy there are many ways to achieve this, one would be passing it with a model to your view. another one would be passing it via a ViewBag/TempData etc... or there is actually easier way just to call method within your view you just need to add @using App.Extensions at the top then call the method within razor. since its static method its easy to achieve.
with VIewBag how can i display it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.