Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
I
Implementation-IdentityServer4
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alan Farhadi
Implementation-IdentityServer4
Commits
95408542
Commit
95408542
authored
May 20, 2020
by
alan.f
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
authorization-with-email
parent
bf7243fa
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
341 additions
and
1 deletion
+341
-1
Authentication.sln
Authentication.sln
+7
-1
HomeController.cs
IdentityExample/Controllers/HomeController.cs
+117
-0
AppDbContext.cs
IdentityExample/Data/AppDbContext.cs
+15
-0
IdentityExample.csproj
IdentityExample/IdentityExample.csproj
+13
-0
Program.cs
IdentityExample/Program.cs
+26
-0
launchSettings.json
IdentityExample/Properties/launchSettings.json
+27
-0
Startup.cs
IdentityExample/Startup.cs
+77
-0
EmailVerification.cshtml
IdentityExample/Views/Home/EmailVerification.cshtml
+5
-0
Index.cshtml
IdentityExample/Views/Home/Index.cshtml
+2
-0
Login.cshtml
IdentityExample/Views/Home/Login.cshtml
+7
-0
Register.cshtml
IdentityExample/Views/Home/Register.cshtml
+7
-0
Secret.cshtml
IdentityExample/Views/Home/Secret.cshtml
+2
-0
VerifyEmail.cshtml
IdentityExample/Views/Home/VerifyEmail.cshtml
+5
-0
appsettings.Development.json
IdentityExample/appsettings.Development.json
+9
-0
appsettings.json
IdentityExample/appsettings.json
+16
-0
libman.json
IdentityExample/libman.json
+6
-0
No files found.
Authentication.sln
View file @
95408542
...
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
...
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
# Visual Studio Version 16
VisualStudioVersion = 16.0.29911.84
VisualStudioVersion = 16.0.29911.84
MinimumVisualStudioVersion = 10.0.40219.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Basics", "Basics\Basics.csproj", "{E2F91297-82C6-4735-B44E-1EAE43684D33}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Basics", "Basics\Basics.csproj", "{E2F91297-82C6-4735-B44E-1EAE43684D33}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentityExample", "IdentityExample\IdentityExample.csproj", "{7CDAF7C0-2118-4FE9-9648-CB7E7D2A0323}"
EndProject
EndProject
Global
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
GlobalSection(SolutionConfigurationPlatforms) = preSolution
...
@@ -15,6 +17,10 @@ Global
...
@@ -15,6 +17,10 @@ Global
{E2F91297-82C6-4735-B44E-1EAE43684D33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2F91297-82C6-4735-B44E-1EAE43684D33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2F91297-82C6-4735-B44E-1EAE43684D33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2F91297-82C6-4735-B44E-1EAE43684D33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2F91297-82C6-4735-B44E-1EAE43684D33}.Release|Any CPU.Build.0 = Release|Any CPU
{E2F91297-82C6-4735-B44E-1EAE43684D33}.Release|Any CPU.Build.0 = Release|Any CPU
{7CDAF7C0-2118-4FE9-9648-CB7E7D2A0323}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7CDAF7C0-2118-4FE9-9648-CB7E7D2A0323}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7CDAF7C0-2118-4FE9-9648-CB7E7D2A0323}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7CDAF7C0-2118-4FE9-9648-CB7E7D2A0323}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
HideSolutionNode = FALSE
...
...
IdentityExample/Controllers/HomeController.cs
0 → 100644
View file @
95408542
using
Microsoft.AspNetCore.Authorization
;
using
Microsoft.AspNetCore.Identity
;
using
Microsoft.AspNetCore.Mvc
;
using
NETCore.MailKit.Core
;
using
System.Threading.Tasks
;
namespace
IdentityExample.Controllers
{
public
class
HomeController
:
Controller
{
private
readonly
UserManager
<
IdentityUser
>
_userManager
;
private
readonly
SignInManager
<
IdentityUser
>
_signInManager
;
private
readonly
IEmailService
_emailService
;
public
HomeController
(
UserManager
<
IdentityUser
>
userManager
,
SignInManager
<
IdentityUser
>
signInManager
,
IEmailService
emailService
)
{
_userManager
=
userManager
;
_signInManager
=
signInManager
;
_emailService
=
emailService
;
}
public
IActionResult
Index
()
{
return
View
();
}
[
Authorize
]
public
IActionResult
Secret
()
{
return
View
();
}
public
IActionResult
Login
()
{
return
View
();
}
[
HttpPost
]
public
async
Task
<
IActionResult
>
Login
(
string
username
,
string
password
)
{
//login functionality
var
user
=
await
_userManager
.
FindByNameAsync
(
username
);
if
(
user
!=
null
)
{
//sign in
var
signInResult
=
await
_signInManager
.
PasswordSignInAsync
(
user
,
password
,
false
,
false
);
if
(
signInResult
.
Succeeded
)
{
return
RedirectToAction
(
"Index"
);
}
}
return
RedirectToAction
(
"Index"
);
}
public
IActionResult
Register
()
{
return
View
();
}
[
HttpPost
]
public
async
Task
<
IActionResult
>
Register
(
string
username
,
string
password
)
{
//register functionality
var
user
=
new
IdentityUser
{
UserName
=
username
,
Email
=
""
,
};
var
result
=
await
_userManager
.
CreateAsync
(
user
,
password
);
if
(
result
.
Succeeded
)
{
//generation of the email token
var
code
=
await
_userManager
.
GenerateEmailConfirmationTokenAsync
(
user
);
var
link
=
Url
.
Action
(
nameof
(
VerifyEmail
),
"Home"
,
new
{
userId
=
user
.
Id
,
code
},
Request
.
Scheme
,
Request
.
Host
.
ToString
());
await
_emailService
.
SendAsync
(
"meh.far.hdi@gmail.com"
,
"email verify"
,
$"<a href=\"
{
link
}
\">Verify Email</a>"
,
true
);
return
RedirectToAction
(
"EmailVerification"
);
}
return
RedirectToAction
(
"Index"
);
}
public
async
Task
<
IActionResult
>
VerifyEmail
(
string
userId
,
string
code
)
{
var
user
=
await
_userManager
.
FindByIdAsync
(
userId
);
if
(
user
==
null
)
return
BadRequest
();
var
result
=
await
_userManager
.
ConfirmEmailAsync
(
user
,
code
);
if
(
result
.
Succeeded
)
{
return
View
();
}
return
BadRequest
();
}
public
IActionResult
EmailVerification
()
=>
View
();
public
async
Task
<
IActionResult
>
LogOut
()
{
await
_signInManager
.
SignOutAsync
();
return
RedirectToAction
(
"Index"
);
}
}
}
IdentityExample/Data/AppDbContext.cs
0 → 100644
View file @
95408542
using
Microsoft.AspNetCore.Identity.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore
;
namespace
IdentityExample.Data
{
// IdentityDbContext contains all the user tables
public
class
AppDbContext
:
IdentityDbContext
{
public
AppDbContext
(
DbContextOptions
<
AppDbContext
>
options
)
:
base
(
options
)
{
}
}
}
IdentityExample/IdentityExample.csproj
0 → 100644
View file @
95408542
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4" />
<PackageReference Include="NETCore.MailKit" Version="2.0.2" />
</ItemGroup>
</Project>
IdentityExample/Program.cs
0 → 100644
View file @
95408542
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.Hosting
;
using
Microsoft.Extensions.Logging
;
namespace
IdentityExample
{
public
class
Program
{
public
static
void
Main
(
string
[]
args
)
{
CreateHostBuilder
(
args
).
Build
().
Run
();
}
public
static
IHostBuilder
CreateHostBuilder
(
string
[]
args
)
=>
Host
.
CreateDefaultBuilder
(
args
)
.
ConfigureWebHostDefaults
(
webBuilder
=>
{
webBuilder
.
UseStartup
<
Startup
>();
});
}
}
IdentityExample/Properties/launchSettings.json
0 → 100644
View file @
95408542
{
"iisSettings"
:
{
"windowsAuthentication"
:
false
,
"anonymousAuthentication"
:
true
,
"iisExpress"
:
{
"applicationUrl"
:
"http://localhost:57417"
,
"sslPort"
:
44304
}
},
"profiles"
:
{
"IIS Express"
:
{
"commandName"
:
"IISExpress"
,
"launchBrowser"
:
true
,
"environmentVariables"
:
{
"ASPNETCORE_ENVIRONMENT"
:
"Development"
}
},
"IdentityExample"
:
{
"commandName"
:
"Project"
,
"launchBrowser"
:
true
,
"applicationUrl"
:
"https://localhost:5001;http://localhost:5000"
,
"environmentVariables"
:
{
"ASPNETCORE_ENVIRONMENT"
:
"Development"
}
}
}
}
IdentityExample/Startup.cs
0 → 100644
View file @
95408542
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
using
IdentityExample.Data
;
using
Microsoft.AspNetCore.Builder
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.AspNetCore.Http
;
using
Microsoft.AspNetCore.Identity
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.Hosting
;
using
NETCore.MailKit.Extensions
;
using
NETCore.MailKit.Infrastructure.Internal
;
namespace
IdentityExample
{
public
class
Startup
{
private
IConfiguration
_config
;
public
Startup
(
IConfiguration
config
)
{
_config
=
config
;
}
public
void
ConfigureServices
(
IServiceCollection
services
)
{
services
.
AddDbContext
<
AppDbContext
>(
config
=>
{
config
.
UseInMemoryDatabase
(
"Memory"
);
});
// AddIdentity registers the services
services
.
AddIdentity
<
IdentityUser
,
IdentityRole
>(
config
=>
{
config
.
Password
.
RequiredLength
=
4
;
config
.
Password
.
RequireDigit
=
false
;
config
.
Password
.
RequireNonAlphanumeric
=
false
;
config
.
Password
.
RequireUppercase
=
false
;
config
.
SignIn
.
RequireConfirmedEmail
=
true
;
})
.
AddEntityFrameworkStores
<
AppDbContext
>()
.
AddDefaultTokenProviders
();
services
.
ConfigureApplicationCookie
(
config
=>
{
config
.
Cookie
.
Name
=
"Identity.Cookie"
;
config
.
LoginPath
=
"/Home/Login"
;
});
services
.
AddMailKit
(
config
=>
config
.
UseMailKit
(
_config
.
GetSection
(
"Email"
).
Get
<
MailKitOptions
>()));
services
.
AddControllersWithViews
();
}
public
void
Configure
(
IApplicationBuilder
app
,
IWebHostEnvironment
env
)
{
if
(
env
.
IsDevelopment
())
{
app
.
UseDeveloperExceptionPage
();
}
app
.
UseRouting
();
app
.
UseAuthentication
();
app
.
UseAuthorization
();
app
.
UseEndpoints
(
endpoints
=>
{
endpoints
.
MapDefaultControllerRoute
();
});
}
}
}
IdentityExample/Views/Home/EmailVerification.cshtml
0 → 100644
View file @
95408542
<h2>Email has been sent</h2>
<h3>Please verify it.</h3>
\ No newline at end of file
IdentityExample/Views/Home/Index.cshtml
0 → 100644
View file @
95408542
<h1>Home Page</h1>
\ No newline at end of file
IdentityExample/Views/Home/Login.cshtml
0 → 100644
View file @
95408542
<h1>Login Page</h1>
<form action="/Home/Login" method="post">
<input type="text" name="username" value="" />
<input type="text" name="password" value="" />
<button type="submit">Login</button>
</form>
IdentityExample/Views/Home/Register.cshtml
0 → 100644
View file @
95408542
<h1>Register Page</h1>
<form action="/Home/Register" method="post">
<input type="text" name="username" value="" />
<input type="text" name="password" value="" />
<button type="submit">Register</button>
</form>
IdentityExample/Views/Home/Secret.cshtml
0 → 100644
View file @
95408542
<h1>Secret Page</h1>
\ No newline at end of file
IdentityExample/Views/Home/VerifyEmail.cshtml
0 → 100644
View file @
95408542
<h2>Thank you for verifying your email please login here</h2>
<a href="/Home/Login">Go to Login </a>
\ No newline at end of file
IdentityExample/appsettings.Development.json
0 → 100644
View file @
95408542
{
"Logging"
:
{
"LogLevel"
:
{
"Default"
:
"Information"
,
"Microsoft"
:
"Warning"
,
"Microsoft.Hosting.Lifetime"
:
"Information"
}
}
}
IdentityExample/appsettings.json
0 → 100644
View file @
95408542
{
"Email"
:
{
"Server"
:
"127.0.0.1"
,
"Port"
:
25
,
"SenderName"
:
"Anton"
,
"SenderEmail"
:
"meh.far.hdi@gmail.com"
},
"Logging"
:
{
"LogLevel"
:
{
"Default"
:
"Information"
,
"Microsoft"
:
"Warning"
,
"Microsoft.Hosting.Lifetime"
:
"Information"
}
},
"AllowedHosts"
:
"*"
}
IdentityExample/libman.json
0 → 100644
View file @
95408542
{
"version"
:
"1.0"
,
"defaultProvider"
:
"cdnjs"
,
"libraries"
:
[]
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment