Commit e9666abe authored by alan.f's avatar alan.f

add-external-providers

parent 35270c61
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
......@@ -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();
}
......
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; }
}
}
......@@ -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; }
}
}
@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
@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" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment