Giter VIP home page Giter VIP logo

vitko.net's Introduction

Vitko.Net

This is my personal library for making ASP.NET Core web apps with Azure integration.

The library abstracts CRUD operations with Azure Blob Storage, CosmosDB, and Active Directory. It also contains some middleware and startup helpers to expedite development.

The current version supports ASP.NET Core 6.40

Installation

Download the package with Nuget.

How to use

Configuration

The included startup helper automatically initializes the required services by looking in appsettings.json.

Three main properties can be configured:

  • ServiceConfig: Specifies which Azure services to use. This is done by specifying EnableBlobService, EnableCosmosService, and EnableUserService with a boolean value.
  • AuthScheme: Within the service config, the scheme for authentication can be specified. This should have the same name as a configuration section, such as AzureAd or AzureAdB2C. If enabled, the user service will also use the specified directory for user management. However, currently only Azure AD B2C is supported for user management.
  • AllowedOrigins: This should be a list of urls that are allowed to access the API. This is used for configuring CORS.

Here is an example appsettings.json file:

{
  "ServiceConfig": {
    "AuthScheme": "AzureAdB2C",
    "EnableBlobService": true,
    "EnableCosmosService": true,
    "EnableUserService": true
  },
  "AllowedOrigins": [
    "https://localhost:3000"
  ],
  "AzureAdB2C": {
    "Instance": "https://login.microsoftonline.com/tfp/",
    "Domain": "mydomain.onmicrosoft.com",
    "ClientId": "myclientid"
  }
}

Startup

To use the startup helper, import the library and run your app in the following way:

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .UseStartup<Startup>()
                    .UseSetting(WebHostDefaults.ApplicationKey, typeof(Program).GetTypeInfo().Assembly.FullName);
            });

Middleware

The startup helper automatically adds middleware to the pipeline for exception handling.

Three exceptions, each extending AbstractApiException, are included:

  • InternalServerException: Will return 500 to the client when thrown
  • BadRequestException: Will return 400 to the client when thrown
  • NotFoundException: Will return 404 to the client when thrown

These exceptions also allow you to specify custom error messages that will be returned to the client. They can be used to more elegantly handle errors in the API.

Services

In your models, you can add code like this to enable CRUD operations with Azure:

private const string ContainerName = "devices";
    private static ICrudService<Device>? _service;
    public static ICrudService<Device> Service
    {
        get
        {
            _service ??= CosmosDbServiceFactory.Instance.CreateService<Device>(ContainerName);
            return _service;
        }

        set => _service = value;
    }

This will create a service that can be used to perform CRUD operations on the model. The service will automatically be initialized with the configuration specified in appsettings.json.

await Device.Service.GetItemAsync("mydeviceid");

For users, you must also add methods for conversion between the user model and the Microsoft Graph user class.

 _service ??= UserServiceFactory.B2CInstance.CreateService<B2CUser>(null, ConvertUserToType, ConvertTypeToUser);
 
 public static explicit operator B2CUser(Microsoft.Graph.User user) {
    return new B2CUser(user.Id, user.DisplayName, user.UserPrincipalName, user.Mail);
 }
 
public static explicit operator Microsoft.Graph.User(B2CUser user) {
    return new Microsoft.Graph.User {
        Id = user.Id,
        DisplayName = user.DisplayName,
        UserPrincipalName = user.Email,
        Mail = user.Email
    };
}

public static B2CUser ConvertUserToType(Microsoft.Graph.User user) {
    return (B2CUser) user;
}

public static Microsoft.Graph.User ConvertTypeToUser(B2CUser user) {
    return (Microsoft.Graph.User) user;
}

vitko.net's People

Contributors

josephvitko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.