Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. var provider = new CookieAuthenticationProvider();
  2. provider.OnApplyRedirect = ctx =>
  3. {
  4. if (ctx.Request.Query["s"] != null)
  5. {
  6. var ticket = LoadTicket(ctx.Request.Query["s"]);
  7. var uri = RemoveQueryStringByKey(ctx.Request.Uri.ToString(), "s");
  8. ticket.Properties.IsPersistent = true;
  9. ctx.OwinContext.Authentication.SignIn(ticket.Properties, ticket.Identity);
  10. ctx.Response.Redirect(uri);
  11. return;
  12. }
  13. }
  14.  
  15. public class SingleSignOnAuthenticationHandler : AuthenticationHandler<AuthenticationOptions>
  16. {
  17. protected override Task<AuthenticationTicket> AuthenticateCoreAsync()
  18. {
  19.  
  20. var sessionIdentifier = Request.Query["s"];
  21. if (sessionIdentifier == null)
  22. return Task.FromResult<AuthenticationTicket>(null);
  23.  
  24. var ticket = LoadTicket(sessionIdentifier);
  25. if (ticket == null)
  26. return Task.FromResult<AuthenticationTicket>(null);
  27.  
  28. Context.Authentication.SignIn(ticket.Properties, ticket.Identity);
  29. return Task.FromResult(new AuthenticationTicket(ticket.Identity, ticket.Properties));
  30. }
  31. }
  32.  
  33. app.UseCookieAuthentication(new CookieAuthenticationOptions
  34. {
  35. AuthenticationType = "ApplicationCookie", // <--- THIS STRING
  36. LoginPath = new PathString(VirtualPathUtility.ToAbsolute("~/Account/Login")),
  37. Provider = provider
  38. });
  39.  
  40. public class SingleSignOnAuthenticationHandler : AuthenticationHandler<AuthenticationOptions>
  41. {
  42. protected override Task<AuthenticationTicket> AuthenticateCoreAsync()
  43. {
  44.  
  45. var sessionIdentifier = Request.Query["s"];
  46. if (sessionIdentifier == null)
  47. return Task.FromResult<AuthenticationTicket>(null);
  48.  
  49. var ticket = LoadTicket(sessionIdentifier);
  50. if (ticket == null)
  51. return Task.FromResult<AuthenticationTicket>(null);
  52.  
  53. ticket.Properties.IsPersistent = true;
  54.  
  55. // ** LOOK HERE **
  56. //new identity, but with the correct authentication type
  57. var identity = new ClaimsIdentity(ticket.Identity.Claims, "ApplicationCookie", ClaimTypes.Name, ClaimTypes.Role);
  58. Context.Authentication.SignIn(ticket.Properties, identity);
  59. return Task.FromResult(new AuthenticationTicket(identity, ticket.Properties));
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement