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

add-signout

parent b086707d
...@@ -45,6 +45,7 @@ namespace IdentityServer ...@@ -45,6 +45,7 @@ namespace IdentityServer
ClientSecrets={ new Secret("client_secret_mvc".ToSha256())}, ClientSecrets={ new Secret("client_secret_mvc".ToSha256())},
AllowedGrantTypes = GrantTypes.Code, AllowedGrantTypes = GrantTypes.Code,
RedirectUris={ "https://localhost:44349/signin-oidc" }, RedirectUris={ "https://localhost:44349/signin-oidc" },
PostLogoutRedirectUris={ "https://localhost:44349/Home/Index" },
AllowedScopes={ AllowedScopes={
"ApiOne", "ApiOne",
"ApiTwo" , "ApiTwo" ,
...@@ -64,6 +65,7 @@ namespace IdentityServer ...@@ -64,6 +65,7 @@ namespace IdentityServer
AllowedGrantTypes = GrantTypes.Implicit, AllowedGrantTypes = GrantTypes.Implicit,
RedirectUris={ "https://localhost:44387/Home/signin" }, RedirectUris={ "https://localhost:44387/Home/signin" },
PostLogoutRedirectUris={ "https://localhost:44387/Home/Idex" },
AllowedCorsOrigins={ "https://localhost:44387" }, AllowedCorsOrigins={ "https://localhost:44387" },
AllowedScopes={ AllowedScopes={
......
...@@ -3,6 +3,7 @@ using System.Collections.Generic; ...@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using IdentityServer.ViewModels; using IdentityServer.ViewModels;
using IdentityServer4.Services;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
...@@ -12,13 +13,16 @@ namespace IdentityServer.Controllers ...@@ -12,13 +13,16 @@ namespace IdentityServer.Controllers
{ {
private readonly SignInManager<IdentityUser> _signInManager; private readonly SignInManager<IdentityUser> _signInManager;
private readonly UserManager<IdentityUser> _userManager; private readonly UserManager<IdentityUser> _userManager;
private readonly IIdentityServerInteractionService _interactionService;
public AuthController( public AuthController(
UserManager<IdentityUser> userManager, UserManager<IdentityUser> userManager,
SignInManager<IdentityUser> signInManager) SignInManager<IdentityUser> signInManager,
IIdentityServerInteractionService interactionService)
{ {
_signInManager = signInManager; _signInManager = signInManager;
_userManager = userManager; _userManager = userManager;
_interactionService = interactionService;
} }
[HttpGet] [HttpGet]
public IActionResult Login(string returnUrl) public IActionResult Login(string returnUrl)
...@@ -26,14 +30,14 @@ namespace IdentityServer.Controllers ...@@ -26,14 +30,14 @@ namespace IdentityServer.Controllers
return View(new LoginViewModel { ReturnUrl =returnUrl}); return View(new LoginViewModel { ReturnUrl =returnUrl});
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> Login(LoginViewModel vm) public async Task<IActionResult> Login(LoginViewModel vm)
{ {
//check if model is exsist //check if model is exsist
var result = await _signInManager.PasswordSignInAsync(vm.Username, vm.Password, false, false); var result =await _signInManager.PasswordSignInAsync(vm.Username, vm.Password, false, false);
if (result.IsNotAllowed)
if (result.Succeeded)
{ {
return Redirect(vm.ReturnUrl); return Redirect(vm.ReturnUrl);
} }
...@@ -44,6 +48,22 @@ namespace IdentityServer.Controllers ...@@ -44,6 +48,22 @@ namespace IdentityServer.Controllers
return View(); return View();
} }
[HttpGet]
public async Task<IActionResult> Logout(string logoutId)
{
await _signInManager.SignOutAsync();
var logoutRequest = await _interactionService.GetLogoutContextAsync(logoutId);
if (string.IsNullOrEmpty(logoutRequest.PostLogoutRedirectUri))
{
return RedirectToAction("Index", "Home");
}
return Redirect(logoutRequest.PostLogoutRedirectUri);
}
[HttpGet] [HttpGet]
public IActionResult Register(string returnUrl) public IActionResult Register(string returnUrl)
{ {
......
...@@ -45,6 +45,7 @@ namespace IdentityServer ...@@ -45,6 +45,7 @@ namespace IdentityServer
{ {
config.Cookie.Name = "IdentityServer.Cookie"; config.Cookie.Name = "IdentityServer.Cookie";
config.LoginPath = "/Auth/Login"; config.LoginPath = "/Auth/Login";
config.LogoutPath = "/Auth/Logout";
}); });
var assembly = typeof(Startup).Assembly.GetName().Name; var assembly = typeof(Startup).Assembly.GetName().Name;
...@@ -59,7 +60,7 @@ namespace IdentityServer ...@@ -59,7 +60,7 @@ namespace IdentityServer
{ {
options.ConfigureDbContext = b => b.UseSqlServer(connectionString, options.ConfigureDbContext = b => b.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(assembly)); sql => sql.MigrationsAssembly(assembly));
}); }).AddDeveloperSigningCredential();
//.AddInMemoryApiResources(Configuration.GetApis()) //.AddInMemoryApiResources(Configuration.GetApis())
//.AddInMemoryIdentityResources(Configuration.GetIdentityResources()) //.AddInMemoryIdentityResources(Configuration.GetIdentityResources())
//.AddInMemoryClients(Configuration.GetClients()) //.AddInMemoryClients(Configuration.GetClients())
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
<button type="submit">Sign In</button> <button type="submit">Sign In</button>
</div> </div>
</form> </form>
@*<div> <div>
<a asp-controller="Auth" asp-action="Register" <a asp-controller="Auth" asp-action="Register"
asp-route-returnUrl="@Model.ReturnUrl">Register</a> asp-route-returnUrl="@Model.ReturnUrl">Register</a>
</div>*@ </div>
\ No newline at end of file \ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
authority: "https://localhost:44325/", authority: "https://localhost:44325/",
client_id: "client_id_js", client_id: "client_id_js",
redirect_uri: "https://localhost:44387/Home/SignIn", redirect_uri: "https://localhost:44387/Home/SignIn",
//post_logout_redirect_uri: "https://localhost:44345/Home/Index", post_logout_redirect_uri: "https://localhost:44387/Home/Index",
response_type: "id_token token", response_type: "id_token token",
scope: "openid rc.scope ApiOne ApiTwo" scope: "openid rc.scope ApiOne ApiTwo"
}; };
...@@ -14,9 +14,9 @@ var signIn = function () { ...@@ -14,9 +14,9 @@ var signIn = function () {
userManager.signinRedirect(); userManager.signinRedirect();
}; };
//var signOut = function () { var signOut = function () {
// userManager.signoutRedirect(); userManager.signoutRedirect();
//}; };
userManager.getUser().then(user => { userManager.getUser().then(user => {
console.log("user:", user); console.log("user:", user);
......
...@@ -25,6 +25,11 @@ namespace MvcClient.Controllers ...@@ -25,6 +25,11 @@ namespace MvcClient.Controllers
return View(); return View();
} }
public IActionResult Logout()
{
return SignOut("Cookie","oidc");
}
[Authorize] [Authorize]
public async Task<IActionResult> Secret() public async Task<IActionResult> Secret()
{ {
......
...@@ -27,7 +27,7 @@ namespace MvcClient ...@@ -27,7 +27,7 @@ namespace MvcClient
config.SaveTokens = true; config.SaveTokens = true;
config.ResponseType = "code"; config.ResponseType = "code";
config.SignedOutCallbackPath = "/Home/Index";
//config cookie claim mapping //config cookie claim mapping
config.ClaimActions.DeleteClaim("amr"); config.ClaimActions.DeleteClaim("amr");
config.ClaimActions.MapUniqueJsonKey("ReadaCoding.Grandma","rc.grandma"); config.ClaimActions.MapUniqueJsonKey("ReadaCoding.Grandma","rc.grandma");
...@@ -42,7 +42,7 @@ namespace MvcClient ...@@ -42,7 +42,7 @@ namespace MvcClient
config.Scope.Add("offline_access"); config.Scope.Add("offline_access");
} ); });
services.AddHttpClient(); services.AddHttpClient();
......
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