Guden.Auth.Api 1.0.3

dotnet add package Guden.Auth.Api --version 1.0.3
                    
NuGet\Install-Package Guden.Auth.Api -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Guden.Auth.Api" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Guden.Auth.Api" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Guden.Auth.Api" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Guden.Auth.Api --version 1.0.3
                    
#r "nuget: Guden.Auth.Api, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Guden.Auth.Api@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Guden.Auth.Api&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Guden.Auth.Api&version=1.0.3
                    
Install as a Cake Tool

These three Nuget packages are built with NET6. Here, we aimed to create JWT tokens, create-update the user, and perform the operations related to the role assigned to the user on the Wep API prepared with NET. Tests were made with the scenario below.

There are 3 types of users in the structure. These are admin, admin+author and author. Admin can perform the operations on the dashboard menu. Author can perform the operations on the posting operations menu. admin+author (Admin + Author) can perform all menu operations assigned to two users.

The roles we define here are defined in the Core_OperationClaims table.

Users registered to the system are added to the Core_Users table. Each user can have multiple roles, at least one.

In the Core_UserOperationClaims table, we keep the role-user relationship. There are menus in the Core_Menus table, and together with the Core_MenuOperationClaimRel table, we keep the information of which role can see which menu. By using this structure, we can bring the menus related to the role that the user belongs to. Implementing Project to WebApi project

!! We added the token update process to the 1.0.7 version, you can update the access token by using the refresh token.

First of all, we are creating a new project. Afterwards, the following selections are made. The shared script is run to create the tables. Additions are made to appsettings for the settings used to create the token and establish MSSQL connection. Nuget packages need to be included in the project. In order to work with these three packages, the following dependencies must be installed. Microsoft.Extensions.DependencyInjection ⇒ 6.0.0 Microsoft.AspNetCore.Authentication.JwtBearer ⇒ 6.0.10 Microsoft.EntityFrameworkCore ⇒ 6.0.10 Microsoft.EntityFrameworkCore.SqlServer ⇒ 6.0.10 Microsoft.IdentityModel.Tokens ⇒ 6.25.0 System.IdentityModel.Tokens.Jwt ⇒ 6.25.0 Finally in program.cs Guden.Lib.Bll .ServiceCollectionModule.SetAuthenticationModuleRegister(builder); set-up is required.

You can now try adding a Controller. Program.Cs

#region

ServiceCollectionModule.SetAuthenticationModuleRegister(builder);

#endregion App Settings

#region { "ConnectionStrings": { "DefaultConnection": "********" }, "TokenOptions": { "Audience": "guden", "Issuer": "ugurcan", "AccessTokenExpiration": 10, "SecurityKey": "Kafaca48Kafaca48Kafaca48Kafaca48Kafaca48Kafaca48", "RefreshTokenExpiration": 12 // RefreshTokenExpiration > AccessTokenExpiration MUST!!! }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" } #endregion #region -- AuthController

public class AuthController : Controller { private IAuthService _authService; private ICore_MenuService _menuService; private IAuthService _authService; private ICore_MenuService _menuService;

public AuthController(IAuthService authService, ICore_MenuService core_MenuService) { _authService = authService; _menuService = core_MenuService; }

[AllowAnonymous] [HttpPost("Login")] public IActionResult Login([FromBody]UserForLoginDto userForLoginDto) { var userToLogin = _authService.Login(userForLoginDto); if (!userToLogin.Success) { return Ok(userToLogin); }

var result = _authService.CreateAccessToken(userToLogin.Data); if (result.Success) {

return Ok(result); }

return Ok(result); } [HttpPost("Register")] public ActionResult Register([FromBody]UserForRegisterDto userForRegisterDto) { //userExists var userExists = _authService.UserExists(userForRegisterDto.Email, null); if (!userExists.Success) { return Ok(userExists); } //user added var registerResult = _authService.Register(userForRegisterDto);

if (registerResult.Success) { return Ok(registerResult); }

return Ok(registerResult); } [HttpPost("UpdateUser")] public ActionResult UpdateUser([FromBody] UserForRegisterDto userForRegisterDto) { //user Exists var userExists = _authService.UserExists(userForRegisterDto.Email, userForRegisterDto.Id); if (!userExists.Success) { return Ok(userExists); } //user added var registerResult = _authService.UpdateUser(userForRegisterDto);

if (registerResult.Success) { return Ok(registerResult); }

return Ok(registerResult); } [HttpGet("GetCore_MenuListByUserId")] [Authorize] public IActionResult GetCore_MenuListByUserId() { int userId = Token.GetUserId(User);

var result = _menuService.GetMenusByUser(userId); if (result.Success) { return Ok(result); } else { return Ok(result); } } [HttpPost("RefreshToken")] public ActionResult RefreshToken([FromBody] TokenDto token) { return Ok(_authService.CreateRefreshToken(token.RefreshToken)); } }

#endregion

#region ||||||||||||||||||||||||||||||||||Table Script ||||||||||||||||||||

CREATE TABLE [dbo].[Advert_MainGroups]( [Id] [int] IDENTITY(1,1) NOT NULL, [MainGroupName] nvarchar NOT NULL, [Icon] nvarchar NULL, [Status] [int] NOT NULL, CONSTRAINT [PK_Advert_MainGroupName] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO

CREATE TABLE [dbo].[Core_MenuOperationClaimRel]( [Id] [int] IDENTITY(1,1) NOT NULL, [MenuId] [int] NULL, [OperationClaimId] [int] NULL, [Status] [int] NULL, CONSTRAINT [PK_Core_MenuOperationClaimRel] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO

CREATE TABLE [dbo].[Core_Menus]( [Id] [int] IDENTITY(1,1) NOT NULL, [MenuName] nvarchar NULL, [Title] nvarchar NULL, [Path] nvarchar NULL, [Icon] nvarchar NULL, [cName] nvarchar NULL, [Status] [int] NULL, CONSTRAINT [PK_Core_Menus] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO

CREATE TABLE [dbo].[Core_OperationClaims]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] nvarchar NOT NULL, CONSTRAINT [PK_Core_OperationClaims] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO

CREATE TABLE [dbo].[Core_UserOperationClaims]( [Id] [int] IDENTITY(1,1) NOT NULL, [UserId] [int] NOT NULL, [OperationClaimId] [int] NOT NULL, CONSTRAINT [PK_Core_UserOperationClaims] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO

CREATE TABLE [dbo].[Core_Users]( [Id] [int] IDENTITY(1,1) NOT NULL, [FirstName] nvarchar NOT NULL, [LastName] nvarchar NOT NULL, [Email] nvarchar NOT NULL, [PasswordSalt] varbinary NOT NULL, [PasswordHash] varbinary NOT NULL, [BirthDate] [date] NULL, [PhoneNumber] nvarchar NULL, [RegDate] [datetime] NOT NULL, [UpdateDate] [datetime] NULL, [Status] [int] NULL, [LastAccessToken] nvarchar NULL, [LastRefreshToken] nvarchar NULL, [LastExpiration] [datetime] NULL, [LastRefreshTokenExpiration] [datetime] NULL, CONSTRAINT [PK_Core_Users] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO

SET IDENTITY_INSERT [dbo].[Core_MenuOperationClaimRel] ON GO INSERT [dbo].[Core_MenuOperationClaimRel] ([Id], [MenuId], [OperationClaimId], [Status]) VALUES (1, 3, 1, 1) GO INSERT [dbo].[Core_MenuOperationClaimRel] ([Id], [MenuId], [OperationClaimId], [Status]) VALUES (2, 2, 2, 1) GO INSERT [dbo].[Core_MenuOperationClaimRel] ([Id], [MenuId], [OperationClaimId], [Status]) VALUES (3, 1, 2, 1) GO INSERT [dbo].[Core_MenuOperationClaimRel] ([Id], [MenuId], [OperationClaimId], [Status]) VALUES (4, 4, 1, 1) GO

SET IDENTITY_INSERT [dbo].[Core_MenuOperationClaimRel] OFF GO SET IDENTITY_INSERT [dbo].[Core_Menus] ON GO INSERT [dbo].[Core_Menus] ([Id], [MenuName], [Title], [Path], [Icon], [cName], [Status]) VALUES (1, N'Post Edit (Gönderi Düzenle)', N'Edit Post', N'...', N'..', N'..', 1) GO INSERT [dbo].[Core_Menus] ([Id], [MenuName], [Title], [Path], [Icon], [cName], [Status]) VALUES (2, N'Post Add(Gönderi Ekle', N'Add Post', N'..', N'..', N'..', 1) GO INSERT [dbo].[Core_Menus] ([Id], [MenuName], [Title], [Path], [Icon], [cName], [Status]) VALUES (3, N'Dashboard (Yönetim Paneli)', N'Dashboard', N'...', N'...', N'...', 1) GO INSERT [dbo].[Core_Menus] ([Id], [MenuName], [Title], [Path], [Icon], [cName], [Status]) VALUES (4, N'Author Add (Yazar Ekle)', N'Author Add', NULL, NULL, NULL, 1) GO

SET IDENTITY_INSERT [dbo].[Core_Menus] OFF GO

SET IDENTITY_INSERT [dbo].[Core_OperationClaims] ON GO INSERT [dbo].[Core_OperationClaims] ([Id], [Name]) VALUES (1, N'Admin') GO INSERT [dbo].[Core_OperationClaims] ([Id], [Name]) VALUES (2, N'Author') GO SET IDENTITY_INSERT [dbo].[Core_OperationClaims] OFF GO

SET IDENTITY_INSERT [dbo].[Core_UserOperationClaims] ON GO INSERT [dbo].[Core_UserOperationClaims] ([Id], [UserId], [OperationClaimId]) VALUES (1, 2, 1) GO INSERT [dbo].[Core_UserOperationClaims] ([Id], [UserId], [OperationClaimId]) VALUES (30, 3, 2) GO INSERT [dbo].[Core_UserOperationClaims] ([Id], [UserId], [OperationClaimId]) VALUES (31, 3, 1) GO INSERT [dbo].[Core_UserOperationClaims] ([Id], [UserId], [OperationClaimId]) VALUES (32, 4, 2) GO SET IDENTITY_INSERT [dbo].[Core_UserOperationClaims] OFF GO

SET IDENTITY_INSERT [dbo].[Core_Users] ON GO INSERT [dbo].[Core_Users] ([Id], [FirstName], [LastName], [Email], [PasswordSalt], [PasswordHash], [BirthDate], [PhoneNumber], [RegDate], [UpdateDate], [Status]) VALUES (2, N'Admin', N'-', N'admin', 0x1642E624F1BDB97396C6B8967CAB0BF30CAE2E13A4DD9D5211C1F333A2AB410CAF656A2AEDC839417ED367947EB68DE5DD7BE77C50C45EEB975B34E4D0B36416F62BA27D14FEF886ED2CBA05A6CD504746B036DB975756C81C0DF4FA80CB1AA968A8CE5777B604AEBA7236E08D1042C702DD868AA6B100A71C9143D5ECF80FF6, 0x076A11F4AAC41B9E492C2EAE493F5586D93C83B9E20B73DFB241A8B6869C7E2941CBE8EBA492CA26615536A25A21CDBAD7A758A6DF445950A3020BC00E2AC172, CAST(N'2000-01-01' AS Date), N'555 555 55 55', CAST(N'2022-08-20T19:20:32.520' AS DateTime), NULL, 1) GO INSERT [dbo].[Core_Users] ([Id], [FirstName], [LastName], [Email], [PasswordSalt], [PasswordHash], [BirthDate], [PhoneNumber], [RegDate], [UpdateDate], [Status]) VALUES (3, N'Admin + Author', N'-', N'adminauthor', 0x9F3B13A2C8A993206D4397D249B7E9C2EE894FC74EA929F8787C9F46E9ABAEEF2E83D088635C898C0A209EA054DCDFF75797CD78CDF07C09533EFE2CA718AC8F4641C00A8458DCFE936BC5DA283C6FB2A635523267BBBD42A574A8C2A49293D871FD479444ADCBD76AA7D4441008DC4AADB12922FEB6AB0EEB0FFE1157F07735, 0x19A7562E0FBF1399FF46B326BE20358C8B1DC707CF2C1729BCA2D29A306DFEC157C33E82A55096905109FC4D58019FC2A5FCBB46D047E4E0B0A23AF06E9BE187, CAST(N'2000-01-01' AS Date), N'555 555 55 55', CAST(N'2022-08-22T11:05:37.933' AS DateTime), CAST(N'2022-08-22T12:13:57.700' AS DateTime), 1) GO INSERT [dbo].[Core_Users] ([Id], [FirstName], [LastName], [Email], [PasswordSalt], [PasswordHash], [BirthDate], [PhoneNumber], [RegDate], [UpdateDate], [Status]) VALUES (4, N'Author', N'-', N'author', 0x371211D149B2112D078C07C325BD79933331296435A678195E4E0FCE897393B62DAFEEC901CA0EF20358B0F215A8283B8C181B968579ECDEF7D4A3CC461C33A59C84E66C21CDE0809AB490071C239FAC8D1C01AFE6CAC89BD3020A85EA719CAAD918EE39194571AB8C31329EFAF12C3F1FBC0CAB339245FE6EAEA8C8188ECD43, 0x007A702C60B8145759D3047C8A9CDC069B14CD07793235490B26EBE52063F44853D552A344221E34104AE87C083EEFB558E1604FD000F263AC00DE416542A274, CAST(N'2000-01-01' AS Date), N'555 555 55 55', CAST(N'2022-08-22T15:17:22.673' AS DateTime), NULL, 1) GO

SET IDENTITY_INSERT [dbo].[Core_Users] OFF GO ALTER TABLE [dbo].[Core_MenuOperationClaimRel] WITH CHECK ADD CONSTRAINT [FK_Core_MenuOperationClaimRel_Core_Menus] FOREIGN KEY([MenuId]) REFERENCES [dbo].[Core_Menus] ([Id]) GO ALTER TABLE [dbo].[Core_MenuOperationClaimRel] CHECK CONSTRAINT [FK_Core_MenuOperationClaimRel_Core_Menus] GO ALTER TABLE [dbo].[Core_MenuOperationClaimRel] WITH CHECK ADD CONSTRAINT [FK_Core_MenuOperationClaimRel_Core_OperationClaims] FOREIGN KEY([OperationClaimId]) REFERENCES [dbo].[Core_OperationClaims] ([Id]) GO ALTER TABLE [dbo].[Core_MenuOperationClaimRel] CHECK CONSTRAINT [FK_Core_MenuOperationClaimRel_Core_OperationClaims] GO ALTER TABLE [dbo].[Core_UserOperationClaims] WITH CHECK ADD CONSTRAINT [FK_Core_UserOperationClaims_Core_OperationClaims] FOREIGN KEY([OperationClaimId]) REFERENCES [dbo].[Core_OperationClaims] ([Id]) GO ALTER TABLE [dbo].[Core_UserOperationClaims] CHECK CONSTRAINT [FK_Core_UserOperationClaims_Core_OperationClaims] GO ALTER TABLE [dbo].[Core_UserOperationClaims] WITH CHECK ADD CONSTRAINT [FK_Core_UserOperationClaims_Core_Users] FOREIGN KEY([UserId]) REFERENCES [dbo].[Core_Users] ([Id]) GO ALTER TABLE [dbo].[Core_UserOperationClaims] CHECK CONSTRAINT [FK_Core_UserOperationClaims_Core_Users] GO

#endregion

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 316 6/11/2023
1.0.2 249 6/11/2023
1.0.1 270 6/11/2023
1.0.0 284 6/1/2023