Guest User

Untitled

a guest
Dec 19th, 2018
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.23 KB | None | 0 0
  1.             // Get the client secret used for signing the tokens
  2.             var keyAsBytes = Encoding.UTF8.GetBytes(Configuration["Auth0:ClientSecret"]);
  3.  
  4.             // if using non-base64 encoded key, just use:
  5.             //var keyAsBase64 = auth0Settings.Value.ClientSecret.Replace('_', '/').Replace('-', '+');
  6.             //var keyAsBytes = Convert.FromBase64String(keyAsBase64);
  7.  
  8.             var issuerSigningKey = new SymmetricSecurityKey(keyAsBytes);
  9.  
  10.             // Add authentication services
  11.             services.AddAuthentication(options =>
  12.                 {
  13.                     options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  14.                     options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  15.                     options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  16.                 })
  17.                 .AddCookie()
  18.                 .AddOpenIdConnect("Auth0", options =>
  19.                 {
  20.                     // Set the authority to your Auth0 domain
  21.                     options.Authority = $"https://{Configuration["Auth0:Domain"]}";
  22.  
  23.                     // Configure the Auth0 Client ID and Client Secret
  24.                     options.ClientId = Configuration["Auth0:ClientId"];
  25.                     options.ClientSecret = Configuration["Auth0:ClientSecret"];
  26.  
  27.                     // Set response type to code
  28.                     options.ResponseType = "code";
  29.                    
  30.                    
  31.                     // https://community.auth0.com/t/authentication-broken-on-asp-net-core-and-safari-on-ios-12-mojave/16077
  32.                     options.ResponseMode = "query";
  33.                    
  34.                     // Configure the scope
  35.                     options.Scope.Clear();
  36.                     options.Scope.Add("openid");
  37.                     options.Scope.Add("user_id");
  38.                     options.Scope.Add("name");
  39.                     options.Scope.Add("profile");
  40.                     options.Scope.Add("email");
  41.  
  42.  
  43.                     // Set the callback path, so Auth0 will call back to http://localhost:5000/signin-auth0
  44.                     // Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard
  45.                     options.CallbackPath = new PathString("/signin-auth0");
  46.                    
  47.                     // Configure the Claims Issuer to be Auth0
  48.                     options.ClaimsIssuer = "Auth0";
  49.  
  50.                     // Set the correct name claim type
  51.                     // FIXME: This doesn't seem to work for some reason
  52.                     options.TokenValidationParameters = new TokenValidationParameters
  53.                     {
  54.                         NameClaimType = "name",
  55.                         RoleClaimType = "https://schemas.scrapify.io/roles"
  56.                     };
  57.                    
  58.                     // manually setup the signature validation key
  59.                     options.TokenValidationParameters = new TokenValidationParameters
  60.                     {
  61.                         IssuerSigningKey = issuerSigningKey
  62.                     };
  63.  
  64.                     options.Events = new OpenIdConnectEvents
  65.                     {
  66.                         // handle the logout redirection
  67.                         OnRedirectToIdentityProviderForSignOut = (context) =>
  68.                         {
  69.                             var logoutUri =
  70.                                 $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}";
  71.  
  72.                             var postLogoutUri = context.Properties.RedirectUri;
  73.                             if (!string.IsNullOrEmpty(postLogoutUri))
  74.                             {
  75.                                 if (postLogoutUri.StartsWith("/"))
  76.                                 {
  77.                                     // transform to absolute
  78.                                     var request = context.Request;
  79.                                     postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase +
  80.                                                     postLogoutUri;
  81.                                 }
  82.  
  83.                                 logoutUri += $"&returnTo={Uri.EscapeDataString(postLogoutUri)}";
  84.                             }
  85.  
  86.                             context.Response.Redirect(logoutUri);
  87.                             context.HandleResponse();
  88.  
  89.                             return Task.CompletedTask;
  90.                         },
  91.                         OnTicketReceived = (context) =>
  92.                         {
  93.                             // stop by `/Account/Continue` instead of going directly to the ReturnUri
  94.                             // to work around Safari's issues with SameSite=lax session cookies not being
  95.                             // returned on the final redirect of the authentication flow.
  96.                             context.ReturnUri = "/Account/Continue?returnUrl="+ System.Net.WebUtility.UrlEncode(context.ReturnUri ?? "/");
  97.                             return Task.CompletedTask;
  98.                         }
  99.                     };
  100.                 });
  101.  
  102.  
  103.             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Add Comment
Please, Sign In to add comment