GithubHelp home page GithubHelp logo

ligass / identity.dapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from grandchamp/identity.dapper

1.0 0.0 0.0 863 KB

Identity package that uses Dapper instead EntityFramework for use with .NET Core

License: MIT License

C# 99.73% Shell 0.12% Dockerfile 0.15%

identity.dapper's Introduction

Identity.Dapper

Build Status NuGet

Find the corresponding NuGet package for your DBMS (Eg: Identity.Dapper.SqlServer).

To configure the DBMS connection, you can add a DapperIdentity and a DapperIdentityCryptography section to your configuration file like this:

"DapperIdentity": {
    "ConnectionString": "Connection string of your database",
    "Username": "user",
    "Password": "123"
},
"DapperIdentityCryptography": {
    "Key": "Base64 32 bytes key",
    "IV": "Base64 16 bytes key"
}

Example:
Key: "E546C8DF278CD5931069B522E695D4F2" (32 Bytes)
Base64 Encoded Key: "RTU0NkM4REYyNzhDRDU5MzEwNjlCNTIyRTY5NUQ0RjI="

IV: "SomeReallyCoolIV" (16 Bytes)
Base64 Encoded IV: "U29tZVJlYWxseUNvb2xJVg=="

Alternatively, you can use ConnectionStrings default section:

"ConnectionStrings": {
    "DefaultConnection": "Connection string of your database"
}

Or you can use the User Secrets commands:

dotnet user-secrets set DapperIdentity:ConnectionString "Connection string of your database"
dotnet user-secrets set DapperIdentity:Password "123"
dotnet user-secrets set DapperIdentity:Username "user"

dotnet user-secrets set DapperIdentityCryptography:Key "Base64 32 bytes key"
dotnet user-secrets set DapperIdentityCryptography:IV "Base64 16 bytes key"

The DapperIdentity:Password can be encrypted with AES256 using the KEY and IV provided.

On Startup.cs file, go to ConfigureServices and add the following lines:

services.ConfigureDapperConnectionProvider<T>(Configuration.GetSection("DapperIdentity"))
        .ConfigureDapperIdentityCryptography(Configuration.GetSection("DapperIdentityCryptography"));

services.AddIdentity<DapperIdentityUser, DapperIdentityRole<int>>()
        .AddDapperIdentityFor<T>()
        .AddDefaultTokenProviders();

or

services.ConfigureDapperConnectionProvider<T>(Configuration.GetSection("ConnectionStrings"))

Where T for the method ConfigureDapperConnectionProvider is DBMSNameConnectionProvider (eg: SqlServerConnectionProvider) and T for the method AddDapperIdentityFor is DBMSNameConfiguration (eg: SqlServerConfiguration).

If you want to use Transactions to all methods of Identity, you'll have to add .ConfigureDapperIdentityOptions(new DapperIdentityOptions { UseTransactionalBehavior = true }) below ConfigureDapperIdentityCryptography(Configuration.GetSection("DapperIdentityCryptography"));

And inside your controller, you'll have to insert on constructor a DapperUserStore<TUser, TKey, TUserRole, TRoleClaim, TUserClaim, TUserLogin, TRole> variable, like this:

private readonly DapperUserStore<CustomUser, int, DapperIdentityUserRole<int>, DapperIdentityRoleClaim<int>, DapperIdentityUserClaim<int>, DapperIdentityUserLogin<int>, CustomRole> _dapperStore;

...

 public ManageController(IUserStore<CustomUser> dapperStore)
        {
            _dapperStore = dapperStore as DapperUserStore<CustomUser, int, DapperIdentityUserRole<int>, DapperIdentityRoleClaim<int>, DapperIdentityUserClaim<int>, DapperIdentityUserLogin<int>, CustomRole>;
        }

And after all operations, you'll have to call DapperUserStore.SaveChanges() method, otherwise your changes will be rollbacked.

Currently, only SQL Server, PostgreSQL and MySQL are supported. We plan support for Oracle when the company release the .NET Core version for their System.Data implementation.

Using Guid as Entity key

Specify the

services.AddIdentity<DapperIdentityUser<Guid>, DapperIdentityRole<Guid>>()
        .AddDapperIdentityFor<T, Guid>();

Changing the default schema (SqlServer)

Pass a custom class that inherits from SqlServerConfiguration (or other)

public class CustomSqlServerConfiguration : SqlServerConfiguration
{
    public CustomSqlServerConfiguration()
    {
        base.SchemaName = "[customSchema]";
    }
}

And add it with

services.AddDapperIdentityFor<CustomSqlServerConfiguration>()

identity.dapper's People

Contributors

grandchamp avatar steine avatar terzano avatar ervwalter avatar peperud avatar techbrij avatar tetsuo13 avatar bgrainger avatar askingalot avatar

Stargazers

 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.