Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
  2. var user = (ClaimsIdentity)context.Principal.Identities.Single(claimsIdentity => claimsIdentity.AuthenticationType == "ecm-qbo");
  3. user.AddClaim(new Claim(type: ClaimTypes.Name, result["givenName"]));
  4. result.ToList().ForEach(kvp => {
  5. user.AddClaim(new Claim(type: kvp.Key, value: kvp.Value, valueType: null, issuer: "qbo", originalIssuer: "qbo", subject: user));
  6. });
  7.  
  8. public void ConfigureServices(IServiceCollection services) {
  9. services.Configure<CookiePolicyOptions>(options => {
  10. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  11. options.CheckConsentNeeded = context => true;
  12. options.MinimumSameSitePolicy = SameSiteMode.None;
  13. });
  14.  
  15. services.ConfigureApplicationCookie(o => {
  16. o.Cookie.Name = "auth_cookie";
  17. o.Cookie.SameSite = SameSiteMode.None;
  18.  
  19. o.Events.OnRedirectToLogin = context => {
  20. context.Response.StatusCode = StatusCodes.Status401Unauthorized;
  21. return Task.CompletedTask;
  22. };
  23. });
  24.  
  25. services.AddAuthentication(o => {
  26. o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  27. o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  28. o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  29. })
  30. .AddOAuth("qbo", "qbo", o => {
  31. o.CallbackPath = new PathString("/signin-qbo");
  32. o.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "sub");
  33. o.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
  34. o.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "givenName");
  35. o.ClaimActions.MapJsonKey(ClaimTypes.Surname, "familyName");
  36. o.ClaimActions.MapJsonKey(ClaimTypes.Name, "givenName");
  37. o.ClientId = Configuration["ecm.qbo.client-id"];
  38. o.ClientSecret = Configuration["ecm.qbo.client-secret"];
  39. o.SaveTokens = true;
  40. o.ClaimsIssuer = "ecm-qbo";
  41. o.Scope.Add("openid");
  42. o.Scope.Add("profile");
  43. o.Scope.Add("email");
  44. o.Scope.Add("com.intuit.quickbooks.accounting");
  45.  
  46. o.AuthorizationEndpoint = Configuration["ecm.qbo.authorization-endpoint"];
  47. o.TokenEndpoint = Configuration["ecm.qbo.token-endpoint"];
  48. o.UserInformationEndpoint = Configuration["ecm.qbo.user-info-endpoint"];
  49.  
  50. o.Events.OnCreatingTicket = async context => {
  51. var companyId = context.Request.Query["realmid"].FirstOrDefault() ?? throw new ArgumentNullException("realmId");
  52. var accessToken = context.AccessToken;
  53. var refreshToken = context.RefreshToken;
  54. var tokens = context.Properties.GetTokens();
  55.  
  56. Configuration["ecm.qbo.access-token"] = accessToken;
  57. Configuration["ecm.qbo.refresh-token"] = refreshToken;
  58. Configuration["ecm.qbo.realm-id"] = companyId;
  59.  
  60. context.Backchannel.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
  61. context.Backchannel.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  62. var response = await context.Backchannel.GetStringAsync(context.Options.UserInformationEndpoint);
  63.  
  64. var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
  65. var user = (ClaimsIdentity)context.Principal.Identities.Single(claimsIdentity => claimsIdentity.AuthenticationType == "ecm-qbo");
  66. user.AddClaim(new Claim(type: ClaimTypes.Name, result["givenName"]));
  67. result.ToList().ForEach(kvp => {
  68. user.AddClaim(new Claim(type: kvp.Key, value: kvp.Value, valueType: null, issuer: "qbo", originalIssuer: "qbo", subject: user));
  69. });
  70.  
  71. await context.HttpContext.SignInAsync(IdentityConstants.ExternalScheme, context.Principal, new AuthenticationProperties {
  72. IsPersistent = false,
  73. RedirectUri = "/"
  74. });
  75. };
  76. });
  77.  
  78. services.AddDbContext<ApplicationDbContext>(options =>
  79. options.UseSqlServer(
  80. Configuration.GetConnectionString("DefaultConnection")));
  81.  
  82. services.AddIdentity<IdentityUser, IdentityRole>()
  83. .AddDefaultUI(UIFramework.Bootstrap4)
  84. .AddEntityFrameworkStores<ApplicationDbContext>();
  85.  
  86. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement