Use this page to detail your site's privacy policy.
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/Views/Identity/AccessDenied.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "AccessDenied";
3 | }
4 |
5 | { "Member", "Admin" };
5 | }
6 |
7 | Signup
8 |
9 |
33 |
34 |
35 | If you already have an account, pelase
sign in
36 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/Views/Shared/Error.cshtml:
--------------------------------------------------------------------------------
1 | @model ErrorViewModel
2 | @{
3 | ViewData["Title"] = "Error";
4 | }
5 |
6 | Error.
7 | An error occurred while processing your request.
8 |
9 | @if (Model.ShowRequestId)
10 | {
11 |
12 | Request ID: @Model.RequestId
13 |
14 | }
15 |
16 | Development Mode
17 |
18 | Swapping to Development environment will display more detailed information about the error that occurred.
19 |
20 |
21 | The Development environment shouldn't be enabled for deployed applications.
22 | It can result in displaying sensitive information from exceptions to end users.
23 | For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development
24 | and restarting the app.
25 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/Views/Shared/_Identity.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | var loggedIn = User.Identity.IsAuthenticated;
3 | }
4 |
5 | @if (loggedIn)
6 | {
7 |
8 | Sign out
9 |
10 |
11 | Hello @User.Identity.Name (@User.Claims.FirstOrDefault(c => c.Type == "Department"))
12 |
13 | }
14 | else
15 | {
16 |
17 | Sign up
18 |
19 |
20 |
21 | Sign in
22 |
23 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/Views/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | @ViewData["Title"] - IdentityNetCore
7 |
8 |
9 |
10 |
11 |
27 |
28 |
29 | @RenderBody()
30 |
31 |
32 |
33 |
38 |
39 |
40 |
41 | @RenderSection("Scripts", false)
42 |
43 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/Views/Shared/_ValidationScriptsPartial.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/Views/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @using IdentityNetCore
2 | @using IdentityNetCore.Models
3 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/Views/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "ConnectionStrings": {
10 | "Default": "Data Source=127.0.0.1;Database=AspnetIdentityV2;User ID=admin;Password=123"
11 | },
12 | "Smtp": {
13 | "Host": "in-v3.mailjet.com",
14 | "Username": "8842993ba6d9d0065e16d718430ddb9a",
15 | "Password": "da9782bab0f04afbd01f6e130c131f44",
16 | "Port": 25
17 | },
18 | "AllowedHosts": "*"
19 | }
20 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/wwwroot/css/site.css:
--------------------------------------------------------------------------------
1 | /* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | for details on configuring this project to bundle and minify static web assets. */
3 |
4 | a.navbar-brand {
5 | white-space: normal;
6 | text-align: center;
7 | word-break: break-all;
8 | }
9 |
10 | /* Provide sufficient contrast against white background */
11 | a {
12 | color: #0366d6;
13 | }
14 |
15 | .btn-primary {
16 | color: #fff;
17 | background-color: #1b6ec2;
18 | border-color: #1861ac;
19 | }
20 |
21 | .nav-pills .nav-link.active, .nav-pills .show > .nav-link {
22 | color: #fff;
23 | background-color: #1b6ec2;
24 | border-color: #1861ac;
25 | }
26 |
27 | /* Sticky footer styles
28 | -------------------------------------------------- */
29 | html {
30 | font-size: 14px;
31 | }
32 |
33 | @media (min-width: 768px) {
34 | html {
35 | font-size: 16px;
36 | }
37 | }
38 |
39 | .border-top {
40 | border-top: 1px solid #e5e5e5;
41 | }
42 |
43 | .border-bottom {
44 | border-bottom: 1px solid #e5e5e5;
45 | }
46 |
47 | .box-shadow {
48 | box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
49 | }
50 |
51 | button.accept-policy {
52 | font-size: 1rem;
53 | line-height: inherit;
54 | }
55 |
56 | /* Sticky footer styles
57 | -------------------------------------------------- */
58 | html {
59 | position: relative;
60 | min-height: 100%;
61 | }
62 |
63 | body {
64 | /* Margin bottom by footer height */
65 | margin-bottom: 60px;
66 | }
67 |
68 | .footer {
69 | position: absolute;
70 | bottom: 0;
71 | width: 100%;
72 | white-space: nowrap;
73 | line-height: 60px; /* Vertically center the text there */
74 | }
75 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/wwwroot/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aussiearef/asp-net-identity-v2/55f2f0f153f32e11fa927947eaa01fd6732a5e28/Cookie-Based-Roles-Claims/IdentityNetCore/wwwroot/favicon.ico
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/wwwroot/js/site.js:
--------------------------------------------------------------------------------
1 | // Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | // for details on configuring this project to bundle and minify static web assets.
3 |
4 | // Write your JavaScript code.
5 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/wwwroot/lib/bootstrap/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2011-2018 Twitter, Inc.
4 | Copyright (c) 2011-2018 The Bootstrap Authors
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/wwwroot/lib/jquery-validation-unobtrusive/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) .NET Foundation. All rights reserved.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 | these files except in compliance with the License. You may obtain a copy of the
5 | License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software distributed
10 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the
12 | specific language governing permissions and limitations under the License.
13 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/wwwroot/lib/jquery-validation/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | =====================
3 |
4 | Copyright Jörn Zaefferer
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles-Claims/IdentityNetCore/wwwroot/lib/jquery/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright JS Foundation and other contributors, https://js.foundation/
2 |
3 | This software consists of voluntary contributions made by many
4 | individuals. For exact contribution history, see the revision history
5 | available at https://github.com/jquery/jquery
6 |
7 | The following license applies to all parts of this software except as
8 | documented below:
9 |
10 | ====
11 |
12 | Permission is hereby granted, free of charge, to any person obtaining
13 | a copy of this software and associated documentation files (the
14 | "Software"), to deal in the Software without restriction, including
15 | without limitation the rights to use, copy, modify, merge, publish,
16 | distribute, sublicense, and/or sell copies of the Software, and to
17 | permit persons to whom the Software is furnished to do so, subject to
18 | the following conditions:
19 |
20 | The above copyright notice and this permission notice shall be
21 | included in all copies or substantial portions of the Software.
22 |
23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 |
31 | ====
32 |
33 | All files located in the node_modules and external directories are
34 | externally maintained libraries used by this software which have their
35 | own licenses; we recommend you read them, as their terms may differ from
36 | the terms above.
37 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Rider ignored files
5 | /contentModel.xml
6 | /projectSettingsUpdater.xml
7 | /.idea.IdentityNetCore.iml
8 | /modules.xml
9 | # Editor-based HTTP Client requests
10 | /httpRequests/
11 | # Datasource local storage ignored files
12 | /dataSources/
13 | /dataSources.local.xml
14 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/indexLayout.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using System.Diagnostics;
2 | using IdentityNetCore.Models;
3 | using Microsoft.AspNetCore.Authorization;
4 | using Microsoft.AspNetCore.Mvc;
5 | using Microsoft.Extensions.Logging;
6 |
7 | namespace IdentityNetCore.Controllers;
8 |
9 | public class HomeController : Controller
10 | {
11 | private readonly ILogger _logger;
12 |
13 | public HomeController(ILogger logger)
14 | {
15 | _logger = logger;
16 | }
17 |
18 | public IActionResult Index()
19 | {
20 | return View();
21 | }
22 |
23 | [Authorize(Roles = "Member")]
24 | public IActionResult Member()
25 | {
26 | return View();
27 | }
28 |
29 | [Authorize(Roles = "Admin")]
30 | public IActionResult Admin()
31 | {
32 | return View();
33 | }
34 |
35 |
36 | public IActionResult Privacy()
37 | {
38 | return View();
39 | }
40 |
41 | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
42 | public IActionResult Error()
43 | {
44 | return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
45 | }
46 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Data/ApplicationDBContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2 | using Microsoft.EntityFrameworkCore;
3 |
4 | namespace IdentityNetCore.Data;
5 |
6 | public class ApplicationDBContext : IdentityDbContext
7 | {
8 | public ApplicationDBContext()
9 | {
10 | }
11 |
12 | public ApplicationDBContext(DbContextOptions options) : base(options)
13 | {
14 | }
15 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/IdentityNetCore.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 |
6 |
7 |
8 |
9 |
10 |
11 | runtime; build; native; contentfiles; analyzers; buildtransitive
12 | all
13 |
14 |
15 |
16 | all
17 | runtime; build; native; contentfiles; analyzers; buildtransitive
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/IdentityNetCore.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29519.87
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentityNetCore", "IdentityNetCore.csproj", "{3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {0C239C2B-365F-4A1F-8675-03185FF7E083}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Models/ErrorViewModel.cs:
--------------------------------------------------------------------------------
1 | namespace IdentityNetCore.Models;
2 |
3 | public class ErrorViewModel
4 | {
5 | public string RequestId { get; set; }
6 |
7 | public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Models/SigninViewModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace IdentityNetCore.Models;
4 |
5 | public class SigninViewModel
6 | {
7 | [Required(ErrorMessage = "User nanme must be provided.")]
8 | [DataType(DataType.EmailAddress)]
9 | public string Username { get; set; }
10 |
11 | [Required(ErrorMessage = "Password must be provided.")]
12 | [DataType(DataType.Password)]
13 | public string Password { get; set; }
14 |
15 | public bool RememberMe { get; set; }
16 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Models/SignupViewModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace IdentityNetCore.Models;
4 |
5 | public class SignupViewModel
6 | {
7 | [Required]
8 | [DataType(DataType.EmailAddress, ErrorMessage = "Email address is missing or invalid.")]
9 | public string Email { get; set; }
10 |
11 | [Required]
12 | [DataType(DataType.Password, ErrorMessage = "Incorrect or missing password.")]
13 | public string Password { get; set; }
14 |
15 | [Required] public string Role { get; set; }
16 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using IdentityNetCore.Data;
3 | using IdentityNetCore.Service;
4 | using Microsoft.AspNetCore.Builder;
5 | using Microsoft.AspNetCore.Identity;
6 | using Microsoft.EntityFrameworkCore;
7 | using Microsoft.Extensions.DependencyInjection;
8 | using Microsoft.Extensions.Hosting;
9 |
10 | var builder = WebApplication.CreateBuilder(args);
11 |
12 | builder.Services.AddDbContext(options =>
13 | {
14 | var connString = builder.Configuration["ConnectionStrings:Default"];
15 | options.UseSqlServer(connString);
16 | });
17 |
18 | builder.Services.AddIdentity()
19 | .AddEntityFrameworkStores();
20 |
21 | builder.Services.Configure(options =>
22 | {
23 | options.Password.RequiredLength = 3;
24 | options.Password.RequireDigit = true;
25 | options.Password.RequireNonAlphanumeric = false;
26 |
27 | options.Lockout.MaxFailedAccessAttempts = 3;
28 | options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
29 |
30 | options.SignIn.RequireConfirmedEmail = false;
31 | });
32 |
33 | builder.Services.ConfigureApplicationCookie(options =>
34 | {
35 | options.LoginPath = "/Identity/Signin";
36 | options.AccessDeniedPath = "/Identity/AccessDenied";
37 | options.ExpireTimeSpan = TimeSpan.FromHours(10);
38 | });
39 |
40 | builder.Services.Configure(builder.Configuration.GetSection("Smtp"));
41 | builder.Services.AddSingleton();
42 |
43 | builder.Services.AddControllersWithViews();
44 |
45 | var app = builder.Build();
46 |
47 | if (app.Environment.IsDevelopment())
48 | app.UseDeveloperExceptionPage();
49 | else
50 | app.UseExceptionHandler("/Home/Error");
51 |
52 | app.UseStaticFiles();
53 | app.UseRouting();
54 | app.UseAuthentication();
55 | app.UseAuthorization();
56 |
57 | app.MapControllerRoute(
58 | "default",
59 | "{controller=Home}/{action=Index}");
60 |
61 | app.Run();
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Service/IEmailSender.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace IdentityNetCore.Service;
4 |
5 | public interface IEmailSender
6 | {
7 | Task SendEmailAsync(string fromAddress, string toAddress, string subject, string message);
8 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Service/SmtpEmailSender.cs:
--------------------------------------------------------------------------------
1 | using System.Net;
2 | using System.Net.Mail;
3 | using System.Threading.Tasks;
4 | using Microsoft.Extensions.Options;
5 |
6 | namespace IdentityNetCore.Service;
7 |
8 | public class SmtpEmailSender : IEmailSender
9 | {
10 | private readonly IOptions options;
11 |
12 | public SmtpEmailSender(IOptions options)
13 | {
14 | this.options = options;
15 | }
16 |
17 | public async Task SendEmailAsync(string fromAddress, string toAddress, string subject, string message)
18 | {
19 | var mailMessage = new MailMessage(fromAddress, toAddress, subject, message);
20 | using (var client = new SmtpClient(options.Value.Host, options.Value.Port)
21 | {
22 | Credentials = new NetworkCredential(options.Value.Username, options.Value.Password)
23 | })
24 | {
25 | await client.SendMailAsync(mailMessage);
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Service/SmtpOptions.cs:
--------------------------------------------------------------------------------
1 | namespace IdentityNetCore.Service;
2 |
3 | public class SmtpOptions
4 | {
5 | public string Host { get; set; }
6 | public string Username { get; set; }
7 | public string Password { get; set; }
8 | public int Port { get; set; }
9 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Home/Admin.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Member";
3 | }
4 |
5 |
8 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Home/Index.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Home Page";
3 | }
4 |
5 |
6 |
Welcome to ASP.NET Core Identity Management
7 |
10 |
11 |
14 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Home/Member.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Member";
3 | }
4 |
5 |
8 |
9 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Home/Privacy.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Privacy Policy";
3 | }
4 | @ViewData["Title"]
5 |
6 | Use this page to detail your site's privacy policy.
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Identity/AccessDenied.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "AccessDenied";
3 | }
4 |
5 | Access Denied
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Identity/Signin.cshtml:
--------------------------------------------------------------------------------
1 | @model SigninViewModel
2 |
3 | @{
4 | ViewData["Title"] = "Signin";
5 | }
6 |
7 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Identity/Signup.cshtml:
--------------------------------------------------------------------------------
1 | @model SignupViewModel
2 | @{
3 | ViewData["Title"] = "Signup";
4 | var roles = new List { "Member", "Admin" };
5 | }
6 |
7 | Signup
8 |
9 |
28 |
29 |
30 | If you already have an account, pelase
sign in
31 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Shared/Error.cshtml:
--------------------------------------------------------------------------------
1 | @model ErrorViewModel
2 | @{
3 | ViewData["Title"] = "Error";
4 | }
5 |
6 | Error.
7 | An error occurred while processing your request.
8 |
9 | @if (Model.ShowRequestId)
10 | {
11 |
12 | Request ID: @Model.RequestId
13 |
14 | }
15 |
16 | Development Mode
17 |
18 | Swapping to Development environment will display more detailed information about the error that occurred.
19 |
20 |
21 | The Development environment shouldn't be enabled for deployed applications.
22 | It can result in displaying sensitive information from exceptions to end users.
23 | For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development
24 | and restarting the app.
25 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Shared/_Identity.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | var loggedIn = User.Identity.IsAuthenticated;
3 | }
4 |
5 | @if (loggedIn)
6 | {
7 |
8 | Sign out
9 |
10 |
11 | Hello @User.Identity.Name
12 |
13 | }
14 | else
15 | {
16 |
17 | Sign up
18 |
19 |
20 |
21 | Sign in
22 |
23 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | @ViewData["Title"] - IdentityNetCore
7 |
8 |
9 |
10 |
11 |
27 |
28 |
29 | @RenderBody()
30 |
31 |
32 |
33 |
38 |
39 |
40 |
41 | @RenderSection("Scripts", false)
42 |
43 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/Shared/_ValidationScriptsPartial.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @using IdentityNetCore
2 | @using IdentityNetCore.Models
3 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/Views/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "ConnectionStrings": {
10 | "Default": "Data Source=127.0.0.1;Database=AspnetIdentityV2;User ID=admin;Password=123"
11 | },
12 | "Smtp": {
13 | "Host": "in-v3.mailjet.com",
14 | "Username": "8842993ba6d9d0065e16d718430ddb9a",
15 | "Password": "da9782bab0f04afbd01f6e130c131f44",
16 | "Port": 25
17 | },
18 | "AllowedHosts": "*"
19 | }
20 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/wwwroot/css/site.css:
--------------------------------------------------------------------------------
1 | /* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | for details on configuring this project to bundle and minify static web assets. */
3 |
4 | a.navbar-brand {
5 | white-space: normal;
6 | text-align: center;
7 | word-break: break-all;
8 | }
9 |
10 | /* Provide sufficient contrast against white background */
11 | a {
12 | color: #0366d6;
13 | }
14 |
15 | .btn-primary {
16 | color: #fff;
17 | background-color: #1b6ec2;
18 | border-color: #1861ac;
19 | }
20 |
21 | .nav-pills .nav-link.active, .nav-pills .show > .nav-link {
22 | color: #fff;
23 | background-color: #1b6ec2;
24 | border-color: #1861ac;
25 | }
26 |
27 | /* Sticky footer styles
28 | -------------------------------------------------- */
29 | html {
30 | font-size: 14px;
31 | }
32 |
33 | @media (min-width: 768px) {
34 | html {
35 | font-size: 16px;
36 | }
37 | }
38 |
39 | .border-top {
40 | border-top: 1px solid #e5e5e5;
41 | }
42 |
43 | .border-bottom {
44 | border-bottom: 1px solid #e5e5e5;
45 | }
46 |
47 | .box-shadow {
48 | box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
49 | }
50 |
51 | button.accept-policy {
52 | font-size: 1rem;
53 | line-height: inherit;
54 | }
55 |
56 | /* Sticky footer styles
57 | -------------------------------------------------- */
58 | html {
59 | position: relative;
60 | min-height: 100%;
61 | }
62 |
63 | body {
64 | /* Margin bottom by footer height */
65 | margin-bottom: 60px;
66 | }
67 |
68 | .footer {
69 | position: absolute;
70 | bottom: 0;
71 | width: 100%;
72 | white-space: nowrap;
73 | line-height: 60px; /* Vertically center the text there */
74 | }
75 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/wwwroot/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aussiearef/asp-net-identity-v2/55f2f0f153f32e11fa927947eaa01fd6732a5e28/Cookie-Based-Roles/IdentityNetCore/wwwroot/favicon.ico
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/wwwroot/js/site.js:
--------------------------------------------------------------------------------
1 | // Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | // for details on configuring this project to bundle and minify static web assets.
3 |
4 | // Write your JavaScript code.
5 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/wwwroot/lib/bootstrap/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2011-2018 Twitter, Inc.
4 | Copyright (c) 2011-2018 The Bootstrap Authors
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/wwwroot/lib/jquery-validation-unobtrusive/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) .NET Foundation. All rights reserved.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 | these files except in compliance with the License. You may obtain a copy of the
5 | License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software distributed
10 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the
12 | specific language governing permissions and limitations under the License.
13 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/wwwroot/lib/jquery-validation/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | =====================
3 |
4 | Copyright Jörn Zaefferer
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Cookie-Based-Roles/IdentityNetCore/wwwroot/lib/jquery/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright JS Foundation and other contributors, https://js.foundation/
2 |
3 | This software consists of voluntary contributions made by many
4 | individuals. For exact contribution history, see the revision history
5 | available at https://github.com/jquery/jquery
6 |
7 | The following license applies to all parts of this software except as
8 | documented below:
9 |
10 | ====
11 |
12 | Permission is hereby granted, free of charge, to any person obtaining
13 | a copy of this software and associated documentation files (the
14 | "Software"), to deal in the Software without restriction, including
15 | without limitation the rights to use, copy, modify, merge, publish,
16 | distribute, sublicense, and/or sell copies of the Software, and to
17 | permit persons to whom the Software is furnished to do so, subject to
18 | the following conditions:
19 |
20 | The above copyright notice and this permission notice shall be
21 | included in all copies or substantial portions of the Software.
22 |
23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 |
31 | ====
32 |
33 | All files located in the node_modules and external directories are
34 | externally maintained libraries used by this software which have their
35 | own licenses; we recommend you read them, as their terms may differ from
36 | the terms above.
37 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Rider ignored files
5 | /.idea.IdentityNetCore.iml
6 | /projectSettingsUpdater.xml
7 | /contentModel.xml
8 | /modules.xml
9 | # Editor-based HTTP Client requests
10 | /httpRequests/
11 | # Datasource local storage ignored files
12 | /dataSources/
13 | /dataSources.local.xml
14 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/indexLayout.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using System.Diagnostics;
2 | using IdentityNetCore.Models;
3 | using Microsoft.AspNetCore.Authorization;
4 | using Microsoft.AspNetCore.Mvc;
5 | using Microsoft.Extensions.Logging;
6 |
7 | namespace IdentityNetCore.Controllers;
8 |
9 | public class HomeController : Controller
10 | {
11 | private readonly ILogger _logger;
12 |
13 | public HomeController(ILogger logger)
14 | {
15 | _logger = logger;
16 | }
17 |
18 | public IActionResult Index()
19 | {
20 | return View();
21 | }
22 |
23 | [Authorize]
24 | public IActionResult Member()
25 | {
26 | return View();
27 | }
28 |
29 | [Authorize]
30 | public IActionResult Admin()
31 | {
32 | return View();
33 | }
34 |
35 |
36 | public IActionResult Privacy()
37 | {
38 | return View();
39 | }
40 |
41 | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
42 | public IActionResult Error()
43 | {
44 | return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
45 | }
46 | }
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Data/ApplicationDBContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2 | using Microsoft.EntityFrameworkCore;
3 |
4 | namespace IdentityNetCore.Data;
5 |
6 | public class ApplicationDbContext : IdentityDbContext
7 | {
8 | public ApplicationDbContext()
9 | {
10 | }
11 |
12 | public ApplicationDbContext(DbContextOptions options) : base(options)
13 | {
14 | }
15 | }
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/IdentityNetCore.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | runtime; build; native; contentfiles; analyzers; buildtransitive
13 | all
14 |
15 |
16 |
17 | all
18 | runtime; build; native; contentfiles; analyzers; buildtransitive
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/IdentityNetCore.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29519.87
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentityNetCore", "IdentityNetCore.csproj", "{3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {0C239C2B-365F-4A1F-8675-03185FF7E083}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Models/ErrorViewModel.cs:
--------------------------------------------------------------------------------
1 | namespace IdentityNetCore.Models;
2 |
3 | public class ErrorViewModel
4 | {
5 | public string RequestId { get; set; }
6 |
7 | public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8 | }
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Models/SigninViewModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace IdentityNetCore.Models;
4 |
5 | public class SigninViewModel
6 | {
7 | [Required(ErrorMessage = "User nanme must be provided.")]
8 | [DataType(DataType.EmailAddress)]
9 | public string Username { get; set; }
10 |
11 | [Required(ErrorMessage = "Password must be provided.")]
12 | [DataType(DataType.Password)]
13 | public string Password { get; set; }
14 |
15 | public bool RememberMe { get; set; }
16 | }
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Models/SignupViewModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace IdentityNetCore.Models;
4 |
5 | public class SignupViewModel
6 | {
7 | [Required]
8 | [DataType(DataType.EmailAddress, ErrorMessage = "Email address is missing or invalid.")]
9 | public string Email { get; set; }
10 |
11 | [Required]
12 | [DataType(DataType.Password, ErrorMessage = "Incorrect or missing password.")]
13 | public string Password { get; set; }
14 | }
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using IdentityNetCore.Data;
3 | using IdentityNetCore.Service;
4 | using Microsoft.AspNetCore.Builder;
5 | using Microsoft.AspNetCore.Identity;
6 | using Microsoft.EntityFrameworkCore;
7 | using Microsoft.Extensions.DependencyInjection;
8 | using Microsoft.Extensions.Hosting;
9 |
10 | var builder = WebApplication.CreateBuilder(args);
11 |
12 |
13 | var connString = builder.Configuration["ConnectionStrings:Default"];
14 | builder.Services.AddDbContext(options => options.UseSqlServer(connString));
15 | builder.Services.AddIdentity().AddEntityFrameworkStores();
16 |
17 |
18 | builder.Services.Configure(options => {
19 | options.Password.RequiredLength = 3;
20 | options.Password.RequireDigit = true;
21 | options.Password.RequireNonAlphanumeric = false;
22 |
23 | options.Lockout.MaxFailedAccessAttempts = 3;
24 | options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
25 |
26 | options.SignIn.RequireConfirmedEmail = true;
27 | });
28 |
29 | builder.Services.ConfigureApplicationCookie(options => {
30 | options.LoginPath = "/Identity/Signin";
31 | options.AccessDeniedPath = "/Identity/AccessDenied";
32 | options.ExpireTimeSpan = TimeSpan.FromHours(10);
33 | });
34 |
35 | builder.Services.Configure(builder.Configuration.GetSection("Smtp"));
36 | builder.Services.AddSingleton();
37 |
38 | builder.Services.AddControllersWithViews();
39 |
40 | var app = builder.Build();
41 |
42 | // Configure the HTTP request pipeline.
43 | if (app.Environment.IsDevelopment())
44 | {
45 | app.UseDeveloperExceptionPage();
46 | }
47 | else
48 | {
49 | app.UseExceptionHandler("/Home/Error");
50 | }
51 |
52 | app.UseStaticFiles();
53 |
54 | app.UseRouting();
55 |
56 | app.UseAuthentication();
57 | app.UseAuthorization();
58 |
59 | app.MapControllerRoute(
60 | name: "default",
61 | pattern: "{controller=Home}/{action=Index}");
62 |
63 | app.Run();
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Service/IEmailSender.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace IdentityNetCore.Service;
4 |
5 | public interface IEmailSender
6 | {
7 | Task SendEmailAsync(string fromAddress, string toAddress, string subject, string message);
8 | }
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Service/SmtpEmailSender.cs:
--------------------------------------------------------------------------------
1 | using System.Net;
2 | using System.Net.Mail;
3 | using System.Threading.Tasks;
4 | using Microsoft.Extensions.Options;
5 |
6 | namespace IdentityNetCore.Service;
7 |
8 | public class SmtpEmailSender : IEmailSender
9 | {
10 | private readonly IOptions options;
11 |
12 | public SmtpEmailSender(IOptions options)
13 | {
14 | this.options = options;
15 | }
16 |
17 | public async Task SendEmailAsync(string fromAddress, string toAddress, string subject, string message)
18 | {
19 | var mailMessage = new MailMessage(fromAddress, toAddress, subject, message);
20 | using (var client = new SmtpClient(options.Value.Host, options.Value.Port)
21 | {
22 | Credentials = new NetworkCredential(options.Value.Username, options.Value.Password)
23 | })
24 | {
25 | await client.SendMailAsync(mailMessage);
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Service/SmtpOptions.cs:
--------------------------------------------------------------------------------
1 | namespace IdentityNetCore.Service;
2 |
3 | public class SmtpOptions
4 | {
5 | public string Host { get; set; }
6 | public string Username { get; set; }
7 | public string Password { get; set; }
8 | public int Port { get; set; }
9 | }
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/Home/Admin.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Member";
3 | }
4 |
5 |
8 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/Home/Index.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Home Page";
3 | }
4 |
5 |
6 |
Welcome to ASP.NET Core Identity Management
7 |
10 |
11 |
14 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/Home/Member.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Member";
3 | }
4 |
5 |
8 |
9 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/Home/Privacy.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Privacy Policy";
3 | }
4 | @ViewData["Title"]
5 |
6 | Use this page to detail your site's privacy policy.
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/Identity/Signin.cshtml:
--------------------------------------------------------------------------------
1 | @model SigninViewModel
2 |
3 | @{
4 | ViewData["Title"] = "Signin";
5 | }
6 |
7 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/Identity/Signup.cshtml:
--------------------------------------------------------------------------------
1 | @model SignupViewModel
2 | @{
3 | ViewData["Title"] = "Signup";
4 | }
5 |
6 | Signup
7 |
8 |
23 |
24 |
25 | If you already have an account, pelase
sign in
26 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/Shared/Error.cshtml:
--------------------------------------------------------------------------------
1 | @model ErrorViewModel
2 | @{
3 | ViewData["Title"] = "Error";
4 | }
5 |
6 | Error.
7 | An error occurred while processing your request.
8 |
9 | @if (Model.ShowRequestId)
10 | {
11 |
12 | Request ID: @Model.RequestId
13 |
14 | }
15 |
16 | Development Mode
17 |
18 | Swapping to Development environment will display more detailed information about the error that occurred.
19 |
20 |
21 | The Development environment shouldn't be enabled for deployed applications.
22 | It can result in displaying sensitive information from exceptions to end users.
23 | For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development
24 | and restarting the app.
25 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | @ViewData["Title"] - IdentityNetCore
7 |
8 |
9 |
10 |
11 |
32 |
33 |
34 | @RenderBody()
35 |
36 |
37 |
38 |
43 |
44 |
45 |
46 | @RenderSection("Scripts", false)
47 |
48 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/Shared/_ValidationScriptsPartial.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @using IdentityNetCore
2 | @using IdentityNetCore.Models
3 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/Views/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "ConnectionStrings": {
10 | "Default": "Data Source=127.0.0.1;Database=AspnetIdentityV2;User ID=aspnet;Password=Sankimsanasd123)_;Encrypt=False"
11 | },
12 | "AllowedHosts": "*"
13 | }
14 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/wwwroot/css/site.css:
--------------------------------------------------------------------------------
1 | /* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | for details on configuring this project to bundle and minify static web assets. */
3 |
4 | a.navbar-brand {
5 | white-space: normal;
6 | text-align: center;
7 | word-break: break-all;
8 | }
9 |
10 | /* Provide sufficient contrast against white background */
11 | a {
12 | color: #0366d6;
13 | }
14 |
15 | .btn-primary {
16 | color: #fff;
17 | background-color: #1b6ec2;
18 | border-color: #1861ac;
19 | }
20 |
21 | .nav-pills .nav-link.active, .nav-pills .show > .nav-link {
22 | color: #fff;
23 | background-color: #1b6ec2;
24 | border-color: #1861ac;
25 | }
26 |
27 | /* Sticky footer styles
28 | -------------------------------------------------- */
29 | html {
30 | font-size: 14px;
31 | }
32 |
33 | @media (min-width: 768px) {
34 | html {
35 | font-size: 16px;
36 | }
37 | }
38 |
39 | .border-top {
40 | border-top: 1px solid #e5e5e5;
41 | }
42 |
43 | .border-bottom {
44 | border-bottom: 1px solid #e5e5e5;
45 | }
46 |
47 | .box-shadow {
48 | box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
49 | }
50 |
51 | button.accept-policy {
52 | font-size: 1rem;
53 | line-height: inherit;
54 | }
55 |
56 | /* Sticky footer styles
57 | -------------------------------------------------- */
58 | html {
59 | position: relative;
60 | min-height: 100%;
61 | }
62 |
63 | body {
64 | /* Margin bottom by footer height */
65 | margin-bottom: 60px;
66 | }
67 |
68 | .footer {
69 | position: absolute;
70 | bottom: 0;
71 | width: 100%;
72 | white-space: nowrap;
73 | line-height: 60px; /* Vertically center the text there */
74 | }
75 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/wwwroot/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aussiearef/asp-net-identity-v2/55f2f0f153f32e11fa927947eaa01fd6732a5e28/Cookie-Based/IdentityNetCore/wwwroot/favicon.ico
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/wwwroot/js/site.js:
--------------------------------------------------------------------------------
1 | // Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | // for details on configuring this project to bundle and minify static web assets.
3 |
4 | // Write your JavaScript code.
5 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/wwwroot/lib/bootstrap/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2011-2018 Twitter, Inc.
4 | Copyright (c) 2011-2018 The Bootstrap Authors
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/wwwroot/lib/jquery-validation-unobtrusive/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) .NET Foundation. All rights reserved.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 | these files except in compliance with the License. You may obtain a copy of the
5 | License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software distributed
10 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the
12 | specific language governing permissions and limitations under the License.
13 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/wwwroot/lib/jquery-validation/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | =====================
3 |
4 | Copyright Jörn Zaefferer
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Cookie-Based/IdentityNetCore/wwwroot/lib/jquery/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright JS Foundation and other contributors, https://js.foundation/
2 |
3 | This software consists of voluntary contributions made by many
4 | individuals. For exact contribution history, see the revision history
5 | available at https://github.com/jquery/jquery
6 |
7 | The following license applies to all parts of this software except as
8 | documented below:
9 |
10 | ====
11 |
12 | Permission is hereby granted, free of charge, to any person obtaining
13 | a copy of this software and associated documentation files (the
14 | "Software"), to deal in the Software without restriction, including
15 | without limitation the rights to use, copy, modify, merge, publish,
16 | distribute, sublicense, and/or sell copies of the Software, and to
17 | permit persons to whom the Software is furnished to do so, subject to
18 | the following conditions:
19 |
20 | The above copyright notice and this permission notice shall be
21 | included in all copies or substantial portions of the Software.
22 |
23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 |
31 | ====
32 |
33 | All files located in the node_modules and external directories are
34 | externally maintained libraries used by this software which have their
35 | own licenses; we recommend you read them, as their terms may differ from
36 | the terms above.
37 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Rider ignored files
5 | /modules.xml
6 | /projectSettingsUpdater.xml
7 | /.idea.IdentityNetCore.iml
8 | /contentModel.xml
9 | # Editor-based HTTP Client requests
10 | /httpRequests/
11 | # Datasource local storage ignored files
12 | /dataSources/
13 | /dataSources.local.xml
14 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/indexLayout.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore.Mvc;
7 | using Microsoft.Extensions.Logging;
8 | using IdentityNetCore.Models;
9 | using Microsoft.AspNetCore.Authorization;
10 |
11 | namespace IdentityNetCore.Controllers
12 | {
13 | public class HomeController : Controller
14 | {
15 | private readonly ILogger _logger;
16 |
17 | public HomeController(ILogger logger)
18 | {
19 | _logger = logger;
20 | }
21 |
22 | public IActionResult Index()
23 | {
24 | return View();
25 | }
26 |
27 | [Authorize]
28 | public IActionResult Member()
29 | {
30 | return View();
31 | }
32 |
33 | [Authorize]
34 | public IActionResult Admin()
35 | {
36 | return View();
37 | }
38 |
39 |
40 | public IActionResult Privacy()
41 | {
42 | return View();
43 | }
44 |
45 | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
46 | public IActionResult Error()
47 | {
48 | return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Data/ApplicationDBContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2 | using Microsoft.EntityFrameworkCore;
3 |
4 | namespace IdentityNetCore.Data
5 | {
6 | public class ApplicationDBContext : IdentityDbContext
7 | {
8 | public ApplicationDBContext()
9 | { }
10 |
11 | public ApplicationDBContext(DbContextOptions options) : base(options)
12 | {
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/IdentityNetCore.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 | 497b882b-95a3-4026-9f49-7b399191392a
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | runtime; build; native; contentfiles; analyzers; buildtransitive
14 | all
15 |
16 |
17 |
18 | all
19 | runtime; build; native; contentfiles; analyzers; buildtransitive
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/IdentityNetCore.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29519.87
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentityNetCore", "IdentityNetCore.csproj", "{3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {0C239C2B-365F-4A1F-8675-03185FF7E083}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Models/ErrorViewModel.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace IdentityNetCore.Models
4 | {
5 | public class ErrorViewModel
6 | {
7 | public string RequestId { get; set; }
8 |
9 | public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Models/SigninViewModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace IdentityNetCore.Models
4 | {
5 | public class SigninViewModel
6 | {
7 | [Required(ErrorMessage ="User nanme must be provided.")]
8 | [DataType(DataType.EmailAddress)]
9 | public string Username { get; set; }
10 |
11 | [Required(ErrorMessage ="Password must be provided.")]
12 | [DataType(DataType.Password)]
13 | public string Password { get; set; }
14 | public bool RememberMe { get; set; }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Models/SignupViewModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace IdentityNetCore.Models
4 | {
5 | public class SignupViewModel
6 | {
7 | [Required]
8 | [DataType(DataType.EmailAddress, ErrorMessage ="Email address is missing or invalid.")]
9 | public string Email { get; set; }
10 |
11 | [Required]
12 | [DataType(DataType.Password, ErrorMessage ="Incorrect or missing password.")]
13 | public string Password { get; set; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using IdentityNetCore.Data;
3 | using IdentityNetCore.Service;
4 | using Microsoft.AspNetCore.Builder;
5 | using Microsoft.AspNetCore.Identity;
6 | using Microsoft.EntityFrameworkCore;
7 | using Microsoft.Extensions.DependencyInjection;
8 | using Microsoft.Extensions.Hosting;
9 |
10 | var builder = WebApplication.CreateBuilder(args);
11 |
12 | // Add services to the container.
13 | builder.Services.AddRazorPages();
14 | builder.Services.AddControllersWithViews();
15 |
16 | var app = builder.Build();
17 |
18 | // Configure the HTTP request pipeline.
19 | if (!app.Environment.IsDevelopment())
20 | {
21 | app.UseExceptionHandler("/Error");
22 | app.UseHsts();
23 | }
24 |
25 | app.UseHttpsRedirection();
26 | app.UseStaticFiles();
27 |
28 | app.UseAuthentication();
29 | app.UseAuthorization();
30 |
31 | app.MapDefaultControllerRoute();
32 | app.MapRazorPages();
33 |
34 | var connString = app.Configuration["ConnectionStrings:Default"];
35 |
36 | var facebookAppId=app.Configuration["FacebookAppId"] ?? "";
37 | var facebookAppSecret= app.Configuration["FacebookAppSecret"] ?? "";
38 | var smtp = builder.Configuration.GetSection("Smtp");
39 |
40 | ConfigureServices(builder.Services);
41 |
42 | app.Run();
43 |
44 | void ConfigureServices(IServiceCollection services)
45 | {
46 | services.AddDbContext(o => o.UseSqlServer(connString));
47 | services.AddIdentity().AddEntityFrameworkStores();
48 |
49 | services.Configure(options => {
50 |
51 | options.Password.RequiredLength = 3;
52 | options.Password.RequireDigit = true;
53 | options.Password.RequireNonAlphanumeric = false;
54 |
55 | options.Lockout.MaxFailedAccessAttempts = 3;
56 | options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
57 |
58 | options.SignIn.RequireConfirmedEmail = true;
59 |
60 | });
61 |
62 | services.ConfigureApplicationCookie(option=> {
63 | option.LoginPath = "/Identity/Signin";
64 | option.AccessDeniedPath = "/Identity/AccessDenied";
65 | option.ExpireTimeSpan = TimeSpan.FromHours(10);
66 | });
67 |
68 | services.AddAuthentication().AddFacebook(options=> {
69 |
70 | options.AppId = facebookAppId;
71 | options.AppSecret = facebookAppSecret;
72 | });
73 |
74 | services.Configure(smtp);
75 |
76 | services.AddSingleton();
77 |
78 | services.AddControllersWithViews();
79 | }
80 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Service/IEmailSender.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace IdentityNetCore.Service
4 | {
5 | public interface IEmailSender
6 | {
7 | Task SendEmailAsync(string fromAddress, string toAddress, string subject, string message);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Service/SmtpEmailSender.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.Options;
2 | using System.Net;
3 | using System.Net.Mail;
4 | using System.Threading.Tasks;
5 |
6 | namespace IdentityNetCore.Service
7 | {
8 | public class SmtpEmailSender : IEmailSender
9 | {
10 | private readonly IOptions options;
11 |
12 | public SmtpEmailSender(IOptions options)
13 | {
14 | this.options = options;
15 | }
16 | public async Task SendEmailAsync(string fromAddress, string toAddress, string subject, string message)
17 | {
18 | var mailMessage = new MailMessage(fromAddress, toAddress, subject, message);
19 | using (var client = new SmtpClient(options.Value.Host, options.Value.Port)
20 | {
21 | Credentials = new NetworkCredential(options.Value.Username, options.Value.Password)
22 | })
23 | {
24 | await client.SendMailAsync(mailMessage);
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Service/SmtpOptions.cs:
--------------------------------------------------------------------------------
1 |
2 | namespace IdentityNetCore.Service
3 | {
4 | public class SmtpOptions
5 | {
6 | public string Host { get; set; }
7 | public string Username { get; set; }
8 | public string Password { get; set; }
9 | public int Port { get; set; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/Home/Admin.cshtml:
--------------------------------------------------------------------------------
1 |
2 | @{
3 | ViewData["Title"] = "Member";
4 | }
5 |
6 |
9 |
12 |
13 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/Home/Index.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Home Page";
3 | }
4 |
5 |
6 |
Welcome to ASP.NET Core Identity Management
7 |
10 |
11 |
14 |
15 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/Home/Member.cshtml:
--------------------------------------------------------------------------------
1 |
2 | @{
3 | ViewData["Title"] = "Member";
4 | }
5 |
6 |
9 |
10 |
13 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/Home/Privacy.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Privacy Policy";
3 | }
4 | @ViewData["Title"]
5 |
6 | Use this page to detail your site's privacy policy.
7 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/Identity/Signin.cshtml:
--------------------------------------------------------------------------------
1 | @model SigninViewModel
2 | @using Microsoft.AspNetCore.Identity;
3 | @inject SignInManager signInManager
4 |
5 | @{
6 | ViewData["Title"] = "Signin";
7 | var providers = await signInManager.GetExternalAuthenticationSchemesAsync();
8 | }
9 |
10 |
29 |
30 |
31 | You can use your social media account to login as well.
32 |
33 |
34 |
43 |
44 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/Identity/Signup.cshtml:
--------------------------------------------------------------------------------
1 | @model SignupViewModel
2 | @{
3 | ViewData["Title"] = "Signup";
4 | }
5 |
6 | Signup
7 |
8 |
23 |
24 |
25 | If you already have an account, pelase
sign in
26 |
27 |
28 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/Shared/Error.cshtml:
--------------------------------------------------------------------------------
1 | @model ErrorViewModel
2 | @{
3 | ViewData["Title"] = "Error";
4 | }
5 |
6 | Error.
7 | An error occurred while processing your request.
8 |
9 | @if (Model.ShowRequestId)
10 | {
11 |
12 | Request ID: @Model.RequestId
13 |
14 | }
15 |
16 | Development Mode
17 |
18 | Swapping to Development environment will display more detailed information about the error that occurred.
19 |
20 |
21 | The Development environment shouldn't be enabled for deployed applications.
22 | It can result in displaying sensitive information from exceptions to end users.
23 | For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development
24 | and restarting the app.
25 |
26 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | @ViewData["Title"] - IdentityNetCore
7 |
8 |
9 |
10 |
11 |
32 |
33 |
34 | @RenderBody()
35 |
36 |
37 |
38 |
43 |
44 |
45 |
46 | @RenderSection("Scripts", required: false)
47 |
48 |
49 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/Shared/_ValidationScriptsPartial.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @using IdentityNetCore
2 | @using IdentityNetCore.Models
3 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/Views/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
4 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "ConnectionStrings": {
10 | "Default": "Data Source=127.0.0.1;Database=AspnetIdentityV2;User ID=admin;Password=123"
11 | },
12 | "Smtp": {
13 | "Host": "",
14 | "Username": "",
15 | "Password": "",
16 | "Port": 25
17 | },
18 | "AllowedHosts": "*"
19 | }
20 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/wwwroot/css/site.css:
--------------------------------------------------------------------------------
1 | /* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | for details on configuring this project to bundle and minify static web assets. */
3 |
4 | a.navbar-brand {
5 | white-space: normal;
6 | text-align: center;
7 | word-break: break-all;
8 | }
9 |
10 | /* Provide sufficient contrast against white background */
11 | a {
12 | color: #0366d6;
13 | }
14 |
15 | .btn-primary {
16 | color: #fff;
17 | background-color: #1b6ec2;
18 | border-color: #1861ac;
19 | }
20 |
21 | .nav-pills .nav-link.active, .nav-pills .show > .nav-link {
22 | color: #fff;
23 | background-color: #1b6ec2;
24 | border-color: #1861ac;
25 | }
26 |
27 | /* Sticky footer styles
28 | -------------------------------------------------- */
29 | html {
30 | font-size: 14px;
31 | }
32 | @media (min-width: 768px) {
33 | html {
34 | font-size: 16px;
35 | }
36 | }
37 |
38 | .border-top {
39 | border-top: 1px solid #e5e5e5;
40 | }
41 | .border-bottom {
42 | border-bottom: 1px solid #e5e5e5;
43 | }
44 |
45 | .box-shadow {
46 | box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
47 | }
48 |
49 | button.accept-policy {
50 | font-size: 1rem;
51 | line-height: inherit;
52 | }
53 |
54 | /* Sticky footer styles
55 | -------------------------------------------------- */
56 | html {
57 | position: relative;
58 | min-height: 100%;
59 | }
60 |
61 | body {
62 | /* Margin bottom by footer height */
63 | margin-bottom: 60px;
64 | }
65 | .footer {
66 | position: absolute;
67 | bottom: 0;
68 | width: 100%;
69 | white-space: nowrap;
70 | line-height: 60px; /* Vertically center the text there */
71 | }
72 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/wwwroot/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aussiearef/asp-net-identity-v2/55f2f0f153f32e11fa927947eaa01fd6732a5e28/Facebook-Auth/Facebook-Auth/IdentityNetCore/wwwroot/favicon.ico
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/wwwroot/js/site.js:
--------------------------------------------------------------------------------
1 | // Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | // for details on configuring this project to bundle and minify static web assets.
3 |
4 | // Write your JavaScript code.
5 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/wwwroot/lib/bootstrap/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2011-2018 Twitter, Inc.
4 | Copyright (c) 2011-2018 The Bootstrap Authors
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/wwwroot/lib/jquery-validation-unobtrusive/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) .NET Foundation. All rights reserved.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 | these files except in compliance with the License. You may obtain a copy of the
5 | License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software distributed
10 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the
12 | specific language governing permissions and limitations under the License.
13 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/wwwroot/lib/jquery-validation/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | =====================
3 |
4 | Copyright Jörn Zaefferer
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Facebook-Auth/Facebook-Auth/IdentityNetCore/wwwroot/lib/jquery/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright JS Foundation and other contributors, https://js.foundation/
2 |
3 | This software consists of voluntary contributions made by many
4 | individuals. For exact contribution history, see the revision history
5 | available at https://github.com/jquery/jquery
6 |
7 | The following license applies to all parts of this software except as
8 | documented below:
9 |
10 | ====
11 |
12 | Permission is hereby granted, free of charge, to any person obtaining
13 | a copy of this software and associated documentation files (the
14 | "Software"), to deal in the Software without restriction, including
15 | without limitation the rights to use, copy, modify, merge, publish,
16 | distribute, sublicense, and/or sell copies of the Software, and to
17 | permit persons to whom the Software is furnished to do so, subject to
18 | the following conditions:
19 |
20 | The above copyright notice and this permission notice shall be
21 | included in all copies or substantial portions of the Software.
22 |
23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 |
31 | ====
32 |
33 | All files located in the node_modules and external directories are
34 | externally maintained libraries used by this software which have their
35 | own licenses; we recommend you read them, as their terms may differ from
36 | the terms above.
37 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # ASP.NET Core Identity Management & Security
3 |
4 | This repository belongs to the below online course about ASP.NET Core's built-in identity and access management system.
5 |
6 | [](https://www.udemy.com/course/aspnet-core-2-security-and-identity-management-with-c/?referralCode=1AA7034B27B8AAA89324)
7 |
8 |
9 | The course also covers OWASP and WAF, including an introduction to third-party WAF systems commonly used in the industry.
10 |
11 |
12 | ## Getting Started
13 |
14 | Please follow the below instructions and steps for each project:
15 |
16 | 1. Create an empty database in SQL Server. The database name used in the course is AspnetIdentityV2. If you name your database something else, please update the connection string in appSettings.json.
17 | 2. Update the appSettings.json with the correct details of the SQL Server database.
18 | 3. Update the appSettings.json with the correct details of the SMTP server when applicable.
19 | 4. Follow the instructions provided in the course and create the database objects using Entity Framework Migrations:
20 | ```bash
21 | dotnet ef migrations add InitialCreate
22 | dotnet ef database update
23 | ```
24 |
25 | [View the course here](https://www.udemy.com/course/aspnet-core-2-security-and-identity-management-with-c/?referralCode=1AA7034B27B8AAA89324)
26 |
27 | ## More Free Courses on YouTube
28 |
29 | [](http://www.youtube.com/@FreeTechnologyLectures)
30 |
31 | Subscribe to the Free Technology and Technology Management Courses channel for free lectures about Coding, DevOps, and Technology Management. [Here is the link to the YouTube channel](http://www.youtube.com/@FreeTechnologyLectures).
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore.Mvc;
7 | using Microsoft.Extensions.Logging;
8 | using IdentityNetCore.Models;
9 |
10 | namespace IdentityNetCore.Controllers
11 | {
12 | public class HomeController : Controller
13 | {
14 | private readonly ILogger _logger;
15 |
16 | public HomeController(ILogger logger)
17 | {
18 | _logger = logger;
19 | }
20 |
21 | public IActionResult Index()
22 | {
23 | return View();
24 | }
25 |
26 | public IActionResult Member()
27 | {
28 | return View();
29 | }
30 |
31 | public IActionResult Admin()
32 | {
33 | return View();
34 | }
35 |
36 |
37 | public IActionResult Privacy()
38 | {
39 | return View();
40 | }
41 |
42 | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
43 | public IActionResult Error()
44 | {
45 | return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Data/ApplicationDbContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2 | using Microsoft.EntityFrameworkCore;
3 |
4 | namespace IdentityNetCore.Data;
5 |
6 | public class ApplicationDbContext: IdentityDbContext
7 | {
8 |
9 | public ApplicationDbContext()
10 | {
11 |
12 | }
13 |
14 | public ApplicationDbContext(DbContextOptions options) : base(options)
15 | {
16 |
17 | }
18 | }
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/IdentityNetCore.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 |
6 |
7 |
8 |
9 |
10 |
11 | all
12 | runtime; build; native; contentfiles; analyzers; buildtransitive
13 |
14 |
15 |
16 | all
17 | runtime; build; native; contentfiles; analyzers; buildtransitive
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/IdentityNetCore.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29519.87
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentityNetCore", "IdentityNetCore.csproj", "{3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {0C239C2B-365F-4A1F-8675-03185FF7E083}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Models/ErrorViewModel.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace IdentityNetCore.Models
4 | {
5 | public class ErrorViewModel
6 | {
7 | public string RequestId { get; set; }
8 |
9 | public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using IdentityNetCore.Data;
3 | using Microsoft.AspNetCore.Builder;
4 | using Microsoft.AspNetCore.Identity;
5 | using Microsoft.EntityFrameworkCore;
6 | using Microsoft.Extensions.DependencyInjection;
7 | using Microsoft.Extensions.Hosting;
8 |
9 | var builder = WebApplication.CreateBuilder(args);
10 |
11 | var connString = builder.Configuration["ConnectionStrings:Default"];
12 |
13 | builder.Services.AddDbContext(options => options.UseSqlServer(connString));
14 | builder.Services.AddIdentity().AddEntityFrameworkStores();
15 |
16 | builder.Services.Configure(options =>
17 | {
18 | options.Password.RequiredLength = 8;
19 | options.Password.RequireDigit = true;
20 | options.Password.RequireNonAlphanumeric = true;
21 |
22 | options.Lockout.MaxFailedAccessAttempts = 3;
23 | options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
24 |
25 | options.SignIn.RequireConfirmedEmail = true;
26 | });
27 |
28 | builder.Services.ConfigureApplicationCookie(options =>
29 | {
30 | options.LoginPath = "/Home/SignIn";
31 | options.AccessDeniedPath = "/Home/AccessDenied";
32 | options.ExpireTimeSpan = TimeSpan.FromHours(1);
33 | });
34 |
35 | builder.Services.AddRazorPages();
36 | builder.Services.AddControllersWithViews();
37 |
38 | var app = builder.Build();
39 |
40 | if (!app.Environment.IsDevelopment())
41 | {
42 | app.UseExceptionHandler("/Home/Error");
43 | app.UseHsts();
44 | }
45 |
46 | app.UseHttpsRedirection();
47 | app.UseStaticFiles();
48 |
49 | app.UseAuthorization();
50 | app.UseAuthentication();
51 |
52 | app.MapDefaultControllerRoute();
53 | app.MapRazorPages();
54 |
55 | app.Run();
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Views/Home/Admin.cshtml:
--------------------------------------------------------------------------------
1 |
2 | @{
3 | ViewData["Title"] = "Member";
4 | }
5 |
6 |
9 |
12 |
13 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Views/Home/Index.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Home Page";
3 | }
4 |
5 |
6 |
Welcome to ASP.NET Core Identity Management
7 |
10 |
11 |
14 |
15 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Views/Home/Member.cshtml:
--------------------------------------------------------------------------------
1 |
2 | @{
3 | ViewData["Title"] = "Member";
4 | }
5 |
6 |
9 |
10 |
13 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Views/Home/Privacy.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Privacy Policy";
3 | }
4 | @ViewData["Title"]
5 |
6 | Use this page to detail your site's privacy policy.
7 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Views/Shared/Error.cshtml:
--------------------------------------------------------------------------------
1 | @model ErrorViewModel
2 | @{
3 | ViewData["Title"] = "Error";
4 | }
5 |
6 | Error.
7 | An error occurred while processing your request.
8 |
9 | @if (Model.ShowRequestId)
10 | {
11 |
12 | Request ID: @Model.RequestId
13 |
14 | }
15 |
16 | Development Mode
17 |
18 | Swapping to Development environment will display more detailed information about the error that occurred.
19 |
20 |
21 | The Development environment shouldn't be enabled for deployed applications.
22 | It can result in displaying sensitive information from exceptions to end users.
23 | For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development
24 | and restarting the app.
25 |
26 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Views/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | @ViewData["Title"] - IdentityNetCore
7 |
8 |
9 |
10 |
11 |
32 |
33 |
34 | @RenderBody()
35 |
36 |
37 |
38 |
43 |
44 |
45 |
46 | @RenderSection("Scripts", required: false)
47 |
48 |
49 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Views/Shared/_ValidationScriptsPartial.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Views/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @using IdentityNetCore
2 | @using IdentityNetCore.Models
3 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/Views/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
4 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "ConnectionStrings": {
10 | "Default": "Data Source=127.0.0.1;Database=AspnetIdentityV2;User ID=aspnet;Password=xxx;Encrypt=False"
11 | },
12 | "AllowedHosts": "*"
13 | }
14 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "ConnectionStrings": {
10 | "Default": "Data Source=127.0.0.1;Database=AspnetIdentityV2;User ID=aspnet;Password=xxxx;Encrypt=False"
11 | },
12 | "AllowedHosts": "*"
13 | }
14 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/wwwroot/css/site.css:
--------------------------------------------------------------------------------
1 | /* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | for details on configuring this project to bundle and minify static web assets. */
3 |
4 | a.navbar-brand {
5 | white-space: normal;
6 | text-align: center;
7 | word-break: break-all;
8 | }
9 |
10 | /* Provide sufficient contrast against white background */
11 | a {
12 | color: #0366d6;
13 | }
14 |
15 | .btn-primary {
16 | color: #fff;
17 | background-color: #1b6ec2;
18 | border-color: #1861ac;
19 | }
20 |
21 | .nav-pills .nav-link.active, .nav-pills .show > .nav-link {
22 | color: #fff;
23 | background-color: #1b6ec2;
24 | border-color: #1861ac;
25 | }
26 |
27 | /* Sticky footer styles
28 | -------------------------------------------------- */
29 | html {
30 | font-size: 14px;
31 | }
32 | @media (min-width: 768px) {
33 | html {
34 | font-size: 16px;
35 | }
36 | }
37 |
38 | .border-top {
39 | border-top: 1px solid #e5e5e5;
40 | }
41 | .border-bottom {
42 | border-bottom: 1px solid #e5e5e5;
43 | }
44 |
45 | .box-shadow {
46 | box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
47 | }
48 |
49 | button.accept-policy {
50 | font-size: 1rem;
51 | line-height: inherit;
52 | }
53 |
54 | /* Sticky footer styles
55 | -------------------------------------------------- */
56 | html {
57 | position: relative;
58 | min-height: 100%;
59 | }
60 |
61 | body {
62 | /* Margin bottom by footer height */
63 | margin-bottom: 60px;
64 | }
65 | .footer {
66 | position: absolute;
67 | bottom: 0;
68 | width: 100%;
69 | white-space: nowrap;
70 | line-height: 60px; /* Vertically center the text there */
71 | }
72 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/wwwroot/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aussiearef/asp-net-identity-v2/55f2f0f153f32e11fa927947eaa01fd6732a5e28/Starter/IdentityNetCore/wwwroot/favicon.ico
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/wwwroot/js/site.js:
--------------------------------------------------------------------------------
1 | // Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | // for details on configuring this project to bundle and minify static web assets.
3 |
4 | // Write your JavaScript code.
5 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/wwwroot/lib/bootstrap/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2011-2018 Twitter, Inc.
4 | Copyright (c) 2011-2018 The Bootstrap Authors
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/wwwroot/lib/jquery-validation-unobtrusive/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) .NET Foundation. All rights reserved.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 | these files except in compliance with the License. You may obtain a copy of the
5 | License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software distributed
10 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the
12 | specific language governing permissions and limitations under the License.
13 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/wwwroot/lib/jquery-validation/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | =====================
3 |
4 | Copyright Jörn Zaefferer
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Starter/IdentityNetCore/wwwroot/lib/jquery/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright JS Foundation and other contributors, https://js.foundation/
2 |
3 | This software consists of voluntary contributions made by many
4 | individuals. For exact contribution history, see the revision history
5 | available at https://github.com/jquery/jquery
6 |
7 | The following license applies to all parts of this software except as
8 | documented below:
9 |
10 | ====
11 |
12 | Permission is hereby granted, free of charge, to any person obtaining
13 | a copy of this software and associated documentation files (the
14 | "Software"), to deal in the Software without restriction, including
15 | without limitation the rights to use, copy, modify, merge, publish,
16 | distribute, sublicense, and/or sell copies of the Software, and to
17 | permit persons to whom the Software is furnished to do so, subject to
18 | the following conditions:
19 |
20 | The above copyright notice and this permission notice shall be
21 | included in all copies or substantial portions of the Software.
22 |
23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 |
31 | ====
32 |
33 | All files located in the node_modules and external directories are
34 | externally maintained libraries used by this software which have their
35 | own licenses; we recommend you read them, as their terms may differ from
36 | the terms above.
37 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aussiearef/asp-net-identity-v2/55f2f0f153f32e11fa927947eaa01fd6732a5e28/Token-Basewd-Web-API/.DS_Store
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Rider ignored files
5 | /.idea.IdentityNetCore.iml
6 | /contentModel.xml
7 | /modules.xml
8 | /projectSettingsUpdater.xml
9 | # Editor-based HTTP Client requests
10 | /httpRequests/
11 | # Datasource local storage ignored files
12 | /dataSources/
13 | /dataSources.local.xml
14 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/indexLayout.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/.idea/.idea.IdentityNetCore/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Controllers/ApiSecurityController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IdentityModel.Tokens.Jwt;
4 | using System.Linq;
5 | using System.Security.Claims;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 | using IdentityNetCore.Models;
9 | using Microsoft.AspNetCore.Authorization;
10 | using Microsoft.AspNetCore.Http;
11 | using Microsoft.AspNetCore.Identity;
12 | using Microsoft.AspNetCore.Mvc;
13 | using Microsoft.Extensions.Configuration;
14 | using Microsoft.IdentityModel.Tokens;
15 |
16 | namespace IdentityNetCore.Controllers
17 | {
18 | [Route("api/[controller]")]
19 | [ApiController]
20 | public class ApiSecurityController : ControllerBase
21 | {
22 | private readonly IConfiguration _configuration;
23 | private readonly SignInManager _signInManager;
24 | private readonly UserManager _userManager;
25 |
26 | public ApiSecurityController(IConfiguration configuration, SignInManager signInManager, UserManager userManager)
27 | {
28 | _configuration = configuration;
29 | _signInManager = signInManager;
30 | _userManager = userManager;
31 | }
32 |
33 | [AllowAnonymous]
34 | [Route(template:"Auth")]
35 | [HttpPost]
36 | public async Task TokenAuth(SigninViewModel model)
37 | {
38 | var issuer = _configuration["Tokens:Issuer"];
39 | var audience = _configuration["Tokens:Audience"];
40 | var key = _configuration["Tokens:Key"];
41 |
42 | if (ModelState.IsValid)
43 | {
44 | var signinResult =
45 | await _signInManager.PasswordSignInAsync(model.Username, model.Password, false, false);
46 | if (signinResult.Succeeded)
47 | {
48 | var user = await _userManager.FindByEmailAsync(model.Username);
49 | if (user != null)
50 | {
51 | var claims = new[]
52 | {
53 | new Claim(JwtRegisteredClaimNames.Email , user.Email),
54 | new Claim(JwtRegisteredClaimNames.Jti , user.Id),
55 | };
56 |
57 | var keyBytes = Encoding.UTF8.GetBytes(key);
58 | var theKey = new SymmetricSecurityKey(keyBytes);
59 | var creds = new SigningCredentials(theKey, SecurityAlgorithms.HmacSha256);
60 | var token = new JwtSecurityToken(issuer, audience, claims, expires: DateTime.Now.AddMinutes(30), signingCredentials: creds);
61 |
62 | return Ok(new {token= new JwtSecurityTokenHandler().WriteToken(token) });
63 | }
64 | }
65 | }
66 |
67 | return BadRequest();
68 | }
69 | }
70 | }
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using System.Diagnostics;
2 | using IdentityNetCore.Models;
3 | using Microsoft.AspNetCore.Authorization;
4 | using Microsoft.AspNetCore.Mvc;
5 |
6 | namespace IdentityNetCore.Controllers;
7 |
8 | public class HomeController : Controller
9 | {
10 | public IActionResult Index()
11 | {
12 | return View();
13 | }
14 |
15 | [Authorize]
16 | public IActionResult Member()
17 | {
18 | return View();
19 | }
20 |
21 | [Authorize]
22 | public IActionResult Admin()
23 | {
24 | return View();
25 | }
26 |
27 |
28 | public IActionResult Privacy()
29 | {
30 | return View();
31 | }
32 |
33 | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
34 | public IActionResult Error()
35 | {
36 | return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
37 | }
38 | }
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Controllers/IdentityController.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using System.Threading.Tasks;
3 | using IdentityNetCore.Models;
4 | using IdentityNetCore.Service;
5 | using Microsoft.AspNetCore.Identity;
6 | using Microsoft.AspNetCore.Mvc;
7 |
8 | namespace IdentityNetCore.Controllers;
9 |
10 | public class IdentityController(
11 | UserManager userManager,
12 | SignInManager signInManager,
13 | IEmailSender emailSender)
14 | : Controller
15 | {
16 | public Task Signup()
17 | {
18 | var model = new SignupViewModel();
19 | return Task.FromResult(View(model));
20 | }
21 |
22 | [HttpPost]
23 | public async Task Signup(SignupViewModel model)
24 | {
25 | if (ModelState.IsValid)
26 | if (await userManager.FindByEmailAsync(model.Email) != null)
27 | {
28 | var user = new IdentityUser
29 | {
30 | Email = model.Email,
31 | UserName = model.Email
32 | };
33 |
34 | var result = await userManager.CreateAsync(user, model.Password);
35 | user = await userManager.FindByEmailAsync(model.Email);
36 |
37 | var token = await userManager.GenerateEmailConfirmationTokenAsync(user);
38 |
39 | if (result.Succeeded)
40 | {
41 | var confirmationLink = Url.ActionLink("ConfirmEmail", "Identity", new { userId = user.Id, token });
42 | await emailSender.SendEmailAsync("info@mydomain.com", user.Email, "Confirm your email address",
43 | confirmationLink);
44 |
45 | return RedirectToAction("Signin");
46 | }
47 |
48 | ModelState.AddModelError("Signup", string.Join("", result.Errors.Select(x => x.Description)));
49 | return View(model);
50 | }
51 |
52 | return View(model);
53 | }
54 |
55 |
56 | public async Task ConfirmEmail(string userId, string token)
57 | {
58 | var user = await userManager.FindByIdAsync(userId);
59 |
60 | var result = await userManager.ConfirmEmailAsync(user, token);
61 | if (result.Succeeded) return RedirectToAction("Signin");
62 |
63 | return new NotFoundResult();
64 | }
65 |
66 | public IActionResult Signin()
67 | {
68 | return View(new SigninViewModel());
69 | }
70 |
71 | [HttpPost]
72 | public async Task Signin(SigninViewModel model)
73 | {
74 | if (ModelState.IsValid)
75 | {
76 | var result =
77 | await signInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, false);
78 | if (result.Succeeded)
79 | return RedirectToAction("Signin");
80 | ModelState.AddModelError("Login", "Cannot login.");
81 | }
82 |
83 | return View(model);
84 | }
85 |
86 | public Task AccessDenied()
87 | {
88 | return Task.FromResult(RedirectToAction("Signin"));
89 | }
90 | }
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Controllers/ProductsController.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using IdentityNetCore.Models;
3 | using Microsoft.AspNetCore.Authentication.JwtBearer;
4 | using Microsoft.AspNetCore.Authorization;
5 | using Microsoft.AspNetCore.Mvc;
6 |
7 | namespace IdentityNetCore.Controllers
8 | {
9 | [Route("api/[controller]")]
10 | [ApiController]
11 | public class ProductsController : ControllerBase
12 | {
13 | [Route(template:"List")]
14 | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
15 | public List GetList()
16 | {
17 | var chair = new Product {Name = "Chair", Price = 100};
18 | var desk = new Product {Name = "Desk", Price = 50};
19 |
20 | return new List {chair, desk};
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Data/ApplicationDBContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2 | using Microsoft.EntityFrameworkCore;
3 |
4 | namespace IdentityNetCore.Data
5 | {
6 | public class ApplicationDBContext : IdentityDbContext
7 | {
8 | public ApplicationDBContext()
9 | { }
10 |
11 | public ApplicationDBContext(DbContextOptions options) : base(options)
12 | {
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/IdentityNetCore.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | runtime; build; native; contentfiles; analyzers; buildtransitive
13 | all
14 |
15 |
16 |
17 | all
18 | runtime; build; native; contentfiles; analyzers; buildtransitive
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/IdentityNetCore.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29519.87
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentityNetCore", "IdentityNetCore.csproj", "{3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3AC3603F-A4EE-4E73-ABBF-A66EFBB2907E}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {0C239C2B-365F-4A1F-8675-03185FF7E083}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Models/ErrorViewModel.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace IdentityNetCore.Models
4 | {
5 | public class ErrorViewModel
6 | {
7 | public string RequestId { get; set; }
8 |
9 | public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Models/Product.cs:
--------------------------------------------------------------------------------
1 | namespace IdentityNetCore.Models
2 | {
3 | public class Product
4 | {
5 | public string Name { get; set; }
6 | public int Price { get; set; }
7 | }
8 | }
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Models/SigninViewModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace IdentityNetCore.Models
4 | {
5 | public class SigninViewModel
6 | {
7 | [Required(ErrorMessage ="User nanme must be provided.")]
8 | [DataType(DataType.EmailAddress)]
9 | public string Username { get; set; }
10 |
11 | [Required(ErrorMessage ="Password must be provided.")]
12 | [DataType(DataType.Password)]
13 | public string Password { get; set; }
14 | public bool RememberMe { get; set; }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Models/SignupViewModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace IdentityNetCore.Models
4 | {
5 | public class SignupViewModel
6 | {
7 | [Required]
8 | [DataType(DataType.EmailAddress, ErrorMessage ="Email address is missing or invalid.")]
9 | public string Email { get; set; }
10 |
11 | [Required]
12 | [DataType(DataType.Password, ErrorMessage ="Incorrect or missing password.")]
13 | public string Password { get; set; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using IdentityNetCore.Data;
4 | using IdentityNetCore.Service;
5 | using Microsoft.AspNetCore.Builder;
6 | using Microsoft.AspNetCore.Identity;
7 | using Microsoft.EntityFrameworkCore;
8 | using Microsoft.Extensions.DependencyInjection;
9 | using Microsoft.Extensions.Hosting;
10 | using Microsoft.IdentityModel.Tokens;
11 |
12 | var builder = WebApplication.CreateBuilder(args);
13 |
14 | // Add services to the container.
15 | builder.Services.AddRazorPages();
16 | builder.Services.AddControllersWithViews();
17 |
18 | var app = builder.Build();
19 |
20 | // Configure the HTTP request pipeline.
21 | if (!app.Environment.IsDevelopment())
22 | {
23 | app.UseExceptionHandler("/Error");
24 | app.UseHsts();
25 | }
26 |
27 | app.UseHttpsRedirection();
28 | app.UseStaticFiles();
29 |
30 | app.UseAuthorization();
31 | app.UseAuthentication();
32 |
33 | app.MapDefaultControllerRoute();
34 | app.MapRazorPages();
35 |
36 | var connString = app.Configuration["ConnectionStrings:Default"];
37 | var smtp = app.Configuration.GetSection("Smtp");
38 |
39 | var issuer = app.Configuration["Tokens:Issuer"] ?? "";
40 | var audience = app.Configuration["Tokens:Audience"] ?? "";
41 | var key = app.Configuration["Tokens:Key"] ?? "";
42 |
43 | ConfigureServices(builder.Services);
44 |
45 | app.Run();
46 |
47 | void ConfigureServices(IServiceCollection services)
48 | {
49 | services.AddDbContext(o => o.UseSqlServer(connString));
50 | services.AddIdentity().AddEntityFrameworkStores();
51 |
52 | services.Configure(options =>
53 | {
54 | options.Password.RequiredLength = 3;
55 | options.Password.RequireDigit = true;
56 | options.Password.RequireNonAlphanumeric = false;
57 |
58 | options.Lockout.MaxFailedAccessAttempts = 3;
59 | options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
60 |
61 | options.SignIn.RequireConfirmedEmail = true;
62 | });
63 |
64 | services.ConfigureApplicationCookie(option =>
65 | {
66 | option.LoginPath = "/Identity/Signin";
67 | option.AccessDeniedPath = "/Identity/AccessDenied";
68 | option.ExpireTimeSpan = TimeSpan.FromHours(10);
69 | });
70 |
71 | services.Configure(smtp);
72 |
73 | services.AddSingleton();
74 |
75 | services.AddControllersWithViews();
76 |
77 |
78 | services.AddAuthentication().AddJwtBearer(options =>
79 | {
80 | options.RequireHttpsMetadata = false;
81 | options.SaveToken = true;
82 | options.TokenValidationParameters = new TokenValidationParameters
83 | {
84 | ValidIssuer = issuer,
85 | ValidAudience = audience,
86 | IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
87 | };
88 | });
89 |
90 | }
91 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Service/IEmailSender.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace IdentityNetCore.Service
4 | {
5 | public interface IEmailSender
6 | {
7 | Task SendEmailAsync(string fromAddress, string toAddress, string subject, string message);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Service/SmtpEmailSender.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.Options;
2 | using System.Net;
3 | using System.Net.Mail;
4 | using System.Threading.Tasks;
5 |
6 | namespace IdentityNetCore.Service
7 | {
8 | public class SmtpEmailSender : IEmailSender
9 | {
10 | private readonly IOptions options;
11 |
12 | public SmtpEmailSender(IOptions options)
13 | {
14 | this.options = options;
15 | }
16 | public async Task SendEmailAsync(string fromAddress, string toAddress, string subject, string message)
17 | {
18 | var mailMessage = new MailMessage(fromAddress, toAddress, subject, message);
19 | using (var client = new SmtpClient(options.Value.Host, options.Value.Port)
20 | {
21 | Credentials = new NetworkCredential(options.Value.Username, options.Value.Password)
22 | })
23 | {
24 | await client.SendMailAsync(mailMessage);
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Service/SmtpOptions.cs:
--------------------------------------------------------------------------------
1 |
2 | namespace IdentityNetCore.Service
3 | {
4 | public class SmtpOptions
5 | {
6 | public string Host { get; set; }
7 | public string Username { get; set; }
8 | public string Password { get; set; }
9 | public int Port { get; set; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/Home/Admin.cshtml:
--------------------------------------------------------------------------------
1 |
2 | @{
3 | ViewData["Title"] = "Member";
4 | }
5 |
6 |
9 |
12 |
13 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/Home/Index.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Home Page";
3 | }
4 |
5 |
6 |
Welcome to ASP.NET Core Identity Management
7 |
10 |
11 |
14 |
15 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/Home/Member.cshtml:
--------------------------------------------------------------------------------
1 |
2 | @{
3 | ViewData["Title"] = "Member";
4 | }
5 |
6 |
9 |
10 |
13 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/Home/Privacy.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Privacy Policy";
3 | }
4 | @ViewData["Title"]
5 |
6 | Use this page to detail your site's privacy policy.
7 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/Identity/Signin.cshtml:
--------------------------------------------------------------------------------
1 | @model SigninViewModel
2 |
3 | @{
4 | ViewData["Title"] = "Signin";
5 | }
6 |
7 |
26 |
27 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/Identity/Signup.cshtml:
--------------------------------------------------------------------------------
1 | @model SignupViewModel
2 | @{
3 | ViewData["Title"] = "Signup";
4 | }
5 |
6 | Signup
7 |
8 |
23 |
24 |
25 | If you already have an account, pelase
sign in
26 |
27 |
28 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/Shared/Error.cshtml:
--------------------------------------------------------------------------------
1 | @model ErrorViewModel
2 | @{
3 | ViewData["Title"] = "Error";
4 | }
5 |
6 | Error.
7 | An error occurred while processing your request.
8 |
9 | @if (Model.ShowRequestId)
10 | {
11 |
12 | Request ID: @Model.RequestId
13 |
14 | }
15 |
16 | Development Mode
17 |
18 | Swapping to Development environment will display more detailed information about the error that occurred.
19 |
20 |
21 | The Development environment shouldn't be enabled for deployed applications.
22 | It can result in displaying sensitive information from exceptions to end users.
23 | For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development
24 | and restarting the app.
25 |
26 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | @ViewData["Title"] - IdentityNetCore
7 |
8 |
9 |
10 |
11 |
32 |
33 |
34 | @RenderBody()
35 |
36 |
37 |
38 |
43 |
44 |
45 |
46 | @RenderSection("Scripts", required: false)
47 |
48 |
49 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/Shared/_ValidationScriptsPartial.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @using IdentityNetCore
2 | @using IdentityNetCore.Models
3 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/Views/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
4 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "ConnectionStrings": {
10 | "Default": "Data Source=127.0.0.1;Database=AspnetIdentityV2;User ID=admin;Password="
11 | },
12 | "Smtp": {
13 | "Host": "",
14 | "Username": "",
15 | "Password": "",
16 | "Port": 25
17 | },
18 |
19 | "Tokens": {
20 | "Issuer": "http://localhost",
21 | "Audience": "http://localhost",
22 | "Key": "0123456789ABCDEF"
23 | },
24 |
25 | "AllowedHosts": "*"
26 | }
27 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/wwwroot/css/site.css:
--------------------------------------------------------------------------------
1 | /* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | for details on configuring this project to bundle and minify static web assets. */
3 |
4 | a.navbar-brand {
5 | white-space: normal;
6 | text-align: center;
7 | word-break: break-all;
8 | }
9 |
10 | /* Provide sufficient contrast against white background */
11 | a {
12 | color: #0366d6;
13 | }
14 |
15 | .btn-primary {
16 | color: #fff;
17 | background-color: #1b6ec2;
18 | border-color: #1861ac;
19 | }
20 |
21 | .nav-pills .nav-link.active, .nav-pills .show > .nav-link {
22 | color: #fff;
23 | background-color: #1b6ec2;
24 | border-color: #1861ac;
25 | }
26 |
27 | /* Sticky footer styles
28 | -------------------------------------------------- */
29 | html {
30 | font-size: 14px;
31 | }
32 | @media (min-width: 768px) {
33 | html {
34 | font-size: 16px;
35 | }
36 | }
37 |
38 | .border-top {
39 | border-top: 1px solid #e5e5e5;
40 | }
41 | .border-bottom {
42 | border-bottom: 1px solid #e5e5e5;
43 | }
44 |
45 | .box-shadow {
46 | box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
47 | }
48 |
49 | button.accept-policy {
50 | font-size: 1rem;
51 | line-height: inherit;
52 | }
53 |
54 | /* Sticky footer styles
55 | -------------------------------------------------- */
56 | html {
57 | position: relative;
58 | min-height: 100%;
59 | }
60 |
61 | body {
62 | /* Margin bottom by footer height */
63 | margin-bottom: 60px;
64 | }
65 | .footer {
66 | position: absolute;
67 | bottom: 0;
68 | width: 100%;
69 | white-space: nowrap;
70 | line-height: 60px; /* Vertically center the text there */
71 | }
72 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/wwwroot/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aussiearef/asp-net-identity-v2/55f2f0f153f32e11fa927947eaa01fd6732a5e28/Token-Basewd-Web-API/IdentityNetCore/wwwroot/favicon.ico
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/wwwroot/js/site.js:
--------------------------------------------------------------------------------
1 | // Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | // for details on configuring this project to bundle and minify static web assets.
3 |
4 | // Write your JavaScript code.
5 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/wwwroot/lib/bootstrap/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2011-2018 Twitter, Inc.
4 | Copyright (c) 2011-2018 The Bootstrap Authors
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/wwwroot/lib/jquery-validation-unobtrusive/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) .NET Foundation. All rights reserved.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 | these files except in compliance with the License. You may obtain a copy of the
5 | License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software distributed
10 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the
12 | specific language governing permissions and limitations under the License.
13 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/wwwroot/lib/jquery-validation/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | =====================
3 |
4 | Copyright Jörn Zaefferer
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Token-Basewd-Web-API/IdentityNetCore/wwwroot/lib/jquery/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright JS Foundation and other contributors, https://js.foundation/
2 |
3 | This software consists of voluntary contributions made by many
4 | individuals. For exact contribution history, see the revision history
5 | available at https://github.com/jquery/jquery
6 |
7 | The following license applies to all parts of this software except as
8 | documented below:
9 |
10 | ====
11 |
12 | Permission is hereby granted, free of charge, to any person obtaining
13 | a copy of this software and associated documentation files (the
14 | "Software"), to deal in the Software without restriction, including
15 | without limitation the rights to use, copy, modify, merge, publish,
16 | distribute, sublicense, and/or sell copies of the Software, and to
17 | permit persons to whom the Software is furnished to do so, subject to
18 | the following conditions:
19 |
20 | The above copyright notice and this permission notice shall be
21 | included in all copies or substantial portions of the Software.
22 |
23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 |
31 | ====
32 |
33 | All files located in the node_modules and external directories are
34 | externally maintained libraries used by this software which have their
35 | own licenses; we recommend you read them, as their terms may differ from
36 | the terms above.
37 |
--------------------------------------------------------------------------------
/auth-service/Data/ApplicationDbContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2 | using Microsoft.EntityFrameworkCore;
3 |
4 | namespace auth_service.Data;
5 |
6 | public class ApplicationDbContext : IdentityDbContext
7 | {
8 | public ApplicationDbContext()
9 | {
10 |
11 | }
12 |
13 | public ApplicationDbContext(DbContextOptions options) : base(options)
14 | {
15 |
16 | }
17 | }
--------------------------------------------------------------------------------
/auth-service/Models/AuthModel.cs:
--------------------------------------------------------------------------------
1 | namespace auth_service.Models;
2 |
3 | public class AuthModel
4 | {
5 | public string? UserName { get; set; }
6 | public string? Password { get; set; }
7 | }
--------------------------------------------------------------------------------
/auth-service/Program.cs:
--------------------------------------------------------------------------------
1 | using System.IdentityModel.Tokens.Jwt;
2 | using System.Security.Claims;
3 | using System.Text;
4 | using auth_service.Data;
5 | using auth_service.Models;
6 | using Microsoft.AspNetCore.Identity;
7 | using Microsoft.EntityFrameworkCore;
8 | using Microsoft.IdentityModel.Tokens;
9 |
10 | var builder = WebApplication.CreateBuilder(args);
11 |
12 | builder.Services.AddCors();
13 |
14 | var connString = builder.Configuration["ConnectionStrings:Default"];
15 |
16 | builder.Services.AddDbContext(o => o.UseSqlServer(connString));
17 | builder.Services.AddIdentity()
18 | .AddEntityFrameworkStores()
19 | .AddDefaultTokenProviders()
20 | .AddSignInManager();
21 |
22 | var app = builder.Build();
23 |
24 | app.UseHttpsRedirection();
25 |
26 | app.MapPost("/auth", async (AuthModel model , SignInManager signinManager)=>
27 | {
28 |
29 | var signinResult = await signinManager.PasswordSignInAsync(model.UserName!, model.Password!, false, true);
30 | if (signinResult.Succeeded)
31 | {
32 | var jwt = GenerateJwt(model.UserName!);
33 | return jwt;
34 | }
35 |
36 |
37 | string GenerateJwt(string userName)
38 | {
39 | var key = app.Configuration["EncryptionKey"] ?? "";
40 | var keyBytes = Encoding.ASCII.GetBytes(key);
41 |
42 | var tokenHandler = new JwtSecurityTokenHandler();
43 | var tokenDescriptor = new SecurityTokenDescriptor()
44 | {
45 | Subject = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, userName) }),
46 | Expires = DateTime.Now.AddDays(1),
47 | SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(keyBytes),
48 | SecurityAlgorithms.HmacSha256Signature)
49 | };
50 |
51 | var token = tokenHandler.CreateToken(tokenDescriptor);
52 | return tokenHandler.WriteToken(token);
53 | }
54 |
55 | // return 403
56 | throw new Exception("Signin was not successful.");
57 | });
58 |
59 | app.MapGet("/validate", async (string token) =>
60 | {
61 | var key = app.Configuration["EncryptionKey"] ?? "";
62 | var keyBytes = Encoding.ASCII.GetBytes(key);
63 |
64 | var tokenHandler = new JwtSecurityTokenHandler();
65 | var validationParameters = new TokenValidationParameters()
66 | {
67 | ValidateIssuerSigningKey = true,
68 | IssuerSigningKey = new SymmetricSecurityKey(keyBytes),
69 | ValidateIssuer = false,
70 | ValidateAudience = false,
71 | RequireExpirationTime = true,
72 | ValidateLifetime = true
73 | };
74 |
75 | var principal = await tokenHandler.ValidateTokenAsync(token, validationParameters);
76 | return principal.Claims;
77 | });
78 |
79 | app.Run();
80 |
81 |
--------------------------------------------------------------------------------
/auth-service/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json.schemastore.org/launchsettings.json",
3 | "iisSettings": {
4 | "windowsAuthentication": false,
5 | "anonymousAuthentication": true,
6 | "iisExpress": {
7 | "applicationUrl": "http://localhost:65029",
8 | "sslPort": 44392
9 | }
10 | },
11 | "profiles": {
12 | "http": {
13 | "commandName": "Project",
14 | "dotnetRunMessages": true,
15 | "launchBrowser": true,
16 | "launchUrl": "swagger",
17 | "applicationUrl": "http://localhost:5195",
18 | "environmentVariables": {
19 | "ASPNETCORE_ENVIRONMENT": "Development"
20 | }
21 | },
22 | "https": {
23 | "commandName": "Project",
24 | "dotnetRunMessages": true,
25 | "launchBrowser": true,
26 | "launchUrl": "swagger",
27 | "applicationUrl": "https://localhost:7175;http://localhost:5195",
28 | "environmentVariables": {
29 | "ASPNETCORE_ENVIRONMENT": "Development"
30 | }
31 | },
32 | "IIS Express": {
33 | "commandName": "IISExpress",
34 | "launchBrowser": true,
35 | "launchUrl": "swagger",
36 | "environmentVariables": {
37 | "ASPNETCORE_ENVIRONMENT": "Development"
38 | }
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/auth-service/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft.AspNetCore": "Warning"
6 | }
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/auth-service/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft.AspNetCore": "Warning"
6 | }
7 | },
8 | "ConnectionStrings": {
9 | "Default": "Data Source=127.0.0.1;Database=AspnetIdentityV2;User ID=aspnet;Password=***;Encrypt=False"
10 | },
11 | "EncryptionKey": "1234567890123456"
12 | ,
13 | "AllowedHosts": "*"
14 | }
15 |
--------------------------------------------------------------------------------
/auth-service/auth-service.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 | enable
6 | enable
7 | False
8 | auth_service
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/auth-service/auth-service.http:
--------------------------------------------------------------------------------
1 | @auth_service_HostAddress = http://localhost:5195
2 |
3 | GET {{auth_service_HostAddress}}/weatherforecast/
4 | Accept: application/json
5 |
6 | ###
7 |
--------------------------------------------------------------------------------