Using UserPrincipal in a Repository class results in null object. .Net 6

Viewed 50
using Directory.Services.AccountManagement;
using HumanResources.JobsContext;
using HumanResources.Models;
using HumanResources.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;

Public Class Repository : Controller
{

[HttpPost]
        public ActionResult PostIndexInternal(IFormFile file, Job job)
        {       
            PrincipalContext pc = new PrincipalContext(ContextType.Domain, "PPS");
            UserPrincipal up = UserPrincipal.FindByIdentity(pc, User.Identity.Name);
            string first = up.GivenName;
            string last = up.Surname;
            string full = first + " " + last;
                
                if (file != null)
                {
                    if (file.Length > 0)
                    {
                        //Get File Name
                        var fileName = Path.GetFileName(file.FileName);
                        //Get File Extension
                        var fileExtension = Path.GetExtension(fileName);
                        //Create New File Name based on AD Credentials
                        var newFileName = String.Concat(user, fileExtension);

                        //Creating new object on the database
                        var fileObject = new InternalJob()
                        {
                            IdJob = job.Id,
                            Title = job.Title,
                            EmployeeID = employeeID,
                            FileName = newFileName,
                            EmployeeName = user,
                            CurrentJobTitle = currentPosition,
                            DateApplied = DateTime.Now,
                            Active = true
                        };
                        using (var target = new MemoryStream())
                        {
                            file.CopyTo(target);
                            fileObject.FileData = target.ToArray();
                        }
                        _hrContext.InternalJobs.Add(fileObject);
                        _hrContext.SaveChanges();
                        return RedirectToAction(nameof(JobsApplied));
                    }
                }
                else if (file == null)
                {
                    string msg = "Please do not forget to select and upload the Resume";
                    return Json(msg);

                }

            return RedirectToAction(nameof(JobsApplied));
        }

}
//This would be in the HomeController
[HttpPost]
public IActionResult Example(IFormFile file, Job job)
{
    Repository repository = new Repository(_context);
    return repository.PostIndexInternal(IFormFile file, Job job);

}   

Everytime I use UserPrincipal in the Controller folder this actually works. But if I were to move the UserPrincipal in the Repository class I get the null reference error. The idea is to use a Repository class to store the code and then run a cleaner code in the controller.Any Ideas would actually help. I am stuck in this part using the UserPrincipal in the Repository class. Repository Class is located in the Models directory. Technically everything else works fine but every time I have tried doing this with userprincipal in Repository class it throws the null reference error.

1 Answers

First this code is a bit confusing. You have your Repository class inheriting from Controller. Those two concepts should live in two different layers of abstraction. Meaning the Repository logic should exist in a data access layer where as controllers exist in the application layer.

With that said, if you need User Principle information, in your HomeController, you should access the UserPrinciple, get the information the repository needs any pass only that information down to the repository. UserPrinciple is an application layer notion and should not be used directly in a repository.

Related