Aspnet Dotnet Notes: Custom Authorization & App_Data

Trying to handle a secure folder where the files are not pushed to the server at deploy. Like all files in wwwroot they are in source control, these will not. The files I'm storing are so big that moving them is a separate process do outside of my application.


get current user inside an MVC controller:

From the Controller base class, you can get the IClaimsPrincipal from the User property
System.Security.Claims.ClaimsPrincipal currentUser = this.User;
You can check the claims directly (without a round trip to the database):
bool IsAdmin = currentUser.IsInRole("Admin");
var id = _userManager.GetUserId(User); // Get user id:
Other fields can be fetched from the database's User entity:
  1. Get the user manager using dependency injection
    private UserManager<ApplicationUser> _userManager;
    
    //class constructor
    public MyController(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }
  2. And use it:
    var user = await _userManager.GetUserAsync(User);
    var email = user.Email;

IIS Errors

  • “Failed to map the path '/'.”  
    • This is when IIS doesn't have access to the folder.
  • MSBUILD : error MSB4025
    • One of the most common issue I run into or as least when all my projects were different version of dotnet core was this issue. Which really doesn't give you a good explanation as to why things aren't working. However here are the most common things I've found it to be the case. 
      • Different runtime versions for example 2.1 vs 2.2 
      • Somethings in the Dll is dependencies
More Stuff on Authorization
Where's App_data?

Awhile back I was working on this idea to have a secure location within my application kinda like a youtube video system but I didn't want the videos to be stored inside wwwroot. 


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Use this code if you want the App_Data folder to be in wwwroot
//string baseDir = env.WebRootPath;

// Use this if you want App_Data off your project root folder
string baseDir = env.ContentRootPath;

AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(baseDir, "App_Data"));
}
Now you can put this code where you need it to get your App_Data folder
string dataDir = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();

Web API Example

Another approach I thought was way not have the api expose a folder and just use a regular authorization. From there it would just be an environment issue and having the folder be readable.
Also using the database to map out the data. I don't have a user specific requirement.

[Route("api/[controller]")]
public class BXLogsController : Controller {
    //GET api/BXlogs/id/blah
    [HttpGet("ID/{id}", Name = "GetL")]
    public IActionResult GetById(string id) { ... }

    //GET api/BXlogs/api/blahapi
    [HttpGet("API/{apiname}", Name = "GetLAPI")]
    public IActionResult GetByAPI(string apiname) { ... }
}

Popular posts from this blog

Installing Windows on Acer chromebook 15 cb3-532

Entity Framework: ToListAynsc & WhereAsync

CSharp Cheatsheet