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
e9666abe
Commit
e9666abe
authored
Jun 01, 2020
by
alan.f
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add-external-providers
parent
35270c61
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
106 additions
and
13 deletions
+106
-13
AuthController.cs
IdentityServer/Controllers/AuthController.cs
+66
-2
Startup.cs
IdentityServer/Startup.cs
+10
-8
ExternalRegisterViewModel.cs
IdentityServer/ViewModels/ExternalRegisterViewModel.cs
+13
-0
LoginViewModel.cs
IdentityServer/ViewModels/LoginViewModel.cs
+1
-1
ExternalRegister.cshtml
IdentityServer/Views/Auth/ExternalRegister.cshtml
+14
-0
Login.cshtml
IdentityServer/Views/Auth/Login.cshtml
+2
-2
No files found.
IdentityServer/Controllers/AuthController.cs
View file @
e9666abe
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Security.Claims
;
using
System.Threading.Tasks
;
using
IdentityServer.ViewModels
;
using
IdentityServer4.Services
;
...
...
@@ -25,9 +26,11 @@ namespace IdentityServer.Controllers
_interactionService
=
interactionService
;
}
[
HttpGet
]
public
IActionResult
Login
(
string
returnUrl
)
public
async
Task
<
IActionResult
>
Login
(
string
returnUrl
)
{
return
View
(
new
LoginViewModel
{
ReturnUrl
=
returnUrl
});
var
externalProviders
=
await
_signInManager
.
GetExternalAuthenticationSchemesAsync
();
return
View
(
new
LoginViewModel
{
ReturnUrl
=
returnUrl
,
ExternalProviders
=
externalProviders
});
}
...
...
@@ -89,5 +92,65 @@ namespace IdentityServer.Controllers
return
View
();
}
public
async
Task
<
IActionResult
>
ExternalLogin
(
string
provider
,
string
returnUrl
)
{
var
redirectUri
=
Url
.
Action
(
nameof
(
ExteranlLoginCallback
),
"Auth"
,
new
{
returnUrl
});
var
properties
=
_signInManager
.
ConfigureExternalAuthenticationProperties
(
provider
,
redirectUri
);
return
Challenge
(
properties
,
provider
);
}
public
async
Task
<
IActionResult
>
ExteranlLoginCallback
(
string
returnUrl
)
{
var
info
=
await
_signInManager
.
GetExternalLoginInfoAsync
();
if
(
info
==
null
)
{
return
RedirectToAction
(
"Login"
);
}
var
result
=
await
_signInManager
.
ExternalLoginSignInAsync
(
info
.
LoginProvider
,
info
.
ProviderKey
,
false
);
if
(
result
.
Succeeded
)
{
return
Redirect
(
returnUrl
);
}
var
username
=
info
.
Principal
.
FindFirst
(
ClaimTypes
.
Name
.
Replace
(
" "
,
"_"
)).
Value
;
return
View
(
"ExternalRegister"
,
new
ExternalRegisterViewModel
{
Username
=
username
,
ReturnUrl
=
returnUrl
});
}
public
async
Task
<
IActionResult
>
ExternalRegister
(
ExternalRegisterViewModel
vm
)
{
var
info
=
await
_signInManager
.
GetExternalLoginInfoAsync
();
if
(
info
==
null
)
{
return
RedirectToAction
(
"Login"
);
}
var
user
=
new
IdentityUser
(
vm
.
Username
);
var
result
=
await
_userManager
.
CreateAsync
(
user
);
if
(!
result
.
Succeeded
)
{
return
View
(
vm
);
}
result
=
await
_userManager
.
AddLoginAsync
(
user
,
info
);
if
(!
result
.
Succeeded
)
{
return
View
(
vm
);
}
await
_signInManager
.
SignInAsync
(
user
,
false
);
return
Redirect
(
vm
.
ReturnUrl
);
}
}
}
\ No newline at end of file
IdentityServer/Startup.cs
View file @
e9666abe
...
...
@@ -52,7 +52,7 @@ namespace IdentityServer
var
assembly
=
typeof
(
Startup
).
Assembly
.
GetName
().
Name
;
var
filePath
=
Path
.
Combine
(
_env
.
ContentRootPath
,
"is_cert.pfx"
);
var
certificate
=
new
X509Certificate2
(
filePath
,
"Cesc007+"
);
//
var certificate = new X509Certificate2(filePath,"Cesc007+");
services
.
AddIdentityServer
()
.
AddAspNetIdentity
<
IdentityUser
>()
.
AddConfigurationStore
(
options
=>
...
...
@@ -64,18 +64,20 @@ namespace IdentityServer
{
options
.
ConfigureDbContext
=
b
=>
b
.
UseSqlServer
(
connectionString
,
sql
=>
sql
.
MigrationsAssembly
(
assembly
));
}).
AddSigningCredential
(
certificate
);
//.AddDeveloperSigningCredential();
})
//.AddSigningCredential(certificate);
.
AddDeveloperSigningCredential
();
//.AddInMemoryApiResources(Configuration.GetApis())
//.AddInMemoryIdentityResources(Configuration.GetIdentityResources())
//.AddInMemoryClients(Configuration.GetClients())
//.AddDeveloperSigningCredential();
//services.AddAuthentication()
// .AddFacebook(config => {
// config.AppId = "3396617443742614";
// config.AppSecret = "secret";
// });
services
.
AddAuthentication
()
.
AddFacebook
(
config
=>
{
config
.
AppId
=
"600315157247054"
;
config
.
AppSecret
=
"71af5f609c1f1986b8e87e162093682b"
;
});
services
.
AddControllersWithViews
();
}
...
...
IdentityServer/ViewModels/ExternalRegisterViewModel.cs
0 → 100644
View file @
e9666abe
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
namespace
IdentityServer.ViewModels
{
public
class
ExternalRegisterViewModel
{
public
string
Username
{
get
;
set
;
}
public
string
ReturnUrl
{
get
;
set
;
}
}
}
IdentityServer/ViewModels/LoginViewModel.cs
View file @
e9666abe
...
...
@@ -12,6 +12,6 @@ namespace IdentityServer.ViewModels
public
string
Password
{
get
;
set
;
}
public
string
ReturnUrl
{
get
;
set
;
}
//
public IEnumerable<AuthenticationScheme> ExternalProviders { get; set; }
public
IEnumerable
<
AuthenticationScheme
>
ExternalProviders
{
get
;
set
;
}
}
}
IdentityServer/Views/Auth/ExternalRegister.cshtml
0 → 100644
View file @
e9666abe
@model ExternalRegisterViewModel
<form asp-controller="Auth" asp-action="ExternalRegister" method="post">
<input type="hidden" asp-for="ReturnUrl" />
<div>
<label>Username</label>
<input asp-for="Username" />
<span asp-validation-for="Username"></span>
</div>
<div>
<button type="submit">Sign Up</button>
</div>
</form>
\ No newline at end of file
IdentityServer/Views/Auth/Login.cshtml
View file @
e9666abe
@model LoginViewModel
<h1>Sign In With</h1>
@*
<form asp-controller="Auth" asp-action="ExternalLogin"
<form asp-controller="Auth" asp-action="ExternalLogin"
asp-route-returnUrl="@Model.ReturnUrl" method="post">
@foreach (var provider in Model.ExternalProviders)
{
...
...
@@ -11,7 +11,7 @@
@provider.Name
</button>
}
</form>
*@
</form>
<form asp-controller="Auth" asp-action="Login" method="post">
<input type="hidden" asp-for="ReturnUrl" />
...
...
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