Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. services.AddAuthentication().AddJwtBearer(cfg =>
  2. {
  3. cfg.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
  4. {
  5. ValidIssuer = _config["Tokens:Issuer"],
  6. ValidAudience = _config["Tokens:Audience"],
  7. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Tokens:Key"]))
  8. };
  9. });
  10.  
  11. jwtHelper = new JwtHelperService();
  12. decodedToken: any;
  13.  
  14. constructor(private http: HttpClient) { }
  15.  
  16. login(userToLogin: UserLogin) {
  17. return this.http.post(this.baseURL + 'login', userToLogin)
  18. .pipe(
  19. map((response: any) => {
  20. const user = response;
  21. if (user) {
  22. localStorage.setItem('token', user.token);
  23. localStorage.setItem('user', JSON.stringify(user.loggedInUser))
  24. this.decodedToken = this.jwtHelper.decodeToken(user.token);
  25. this.currentUser = JSON.parse(localStorage.getItem('user'));
  26. }
  27. }));
  28. }
  29.  
  30. export function tokenGetter() {
  31. return localStorage.getItem('token');
  32. }
  33.  
  34. imports: [
  35. JwtModule.forRoot({
  36. config: {
  37. tokenGetter: tokenGetter,
  38. whitelistedDomains: ['localhost:44354/api/'],
  39. blacklistedRoutes: ['localhost:44354/api/account/']
  40. }
  41. }),
  42. ]
  43.  
  44. [Authorize(AuthenticationSchemes=JwtBearerDefaults.AuthenticationScheme)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement