RickStrahl / Westwind.Globalization

Database driven resource localization for .NET applications
544 stars 135 forks source link

How can I add JWT Token Authentication to LocalizationAdmin Page #203

Closed gunduzemreozer closed 3 years ago

gunduzemreozer commented 3 years ago

Hello.

I am a Vue developer and I don't know angular unfortunately. I saw that there is a solution to protect /localizationadmin ui like below.

`services.AddWestwindGlobalization(opt => {
// the default settings comme from DbResourceConfiguration.json if exists // Customize opt with any customizations here...

// *** THIS ***
// Set up security for Localization Administration form
opt.ConfigureAuthorizeLocalizationAdministration(controllerContext =>
{
    // return true or false whether this request is authorized
    return controllerContext.HttpContext.User.Identity.IsAuthenticated;
});

});`

But I use JwtBearer Authentication in my project. I am sending jwt token at request header for authorized end points. For that reason, I don't know how to implement token authorization for HttpContext.User.Identity.IsAuthenticated property. Is there any way to pass token in header at angular side. Or is there any alternative solution to make localizationadmin secure. I'm waiting your suggestions.

gunduzemreozer commented 3 years ago

I searched angular V1 and found solution :) I want to share if someone needs this.

You can add app.config part to the LocalizationAdmin/app/app.js file like below. I added to the place before of other app.config part.


app.config(["$httpProvider", function ($httpProvider) {
      $httpProvider.defaults.headers.common["Accept-Language"] = "en-US";

      var token = localStorage.getItem('token'); // you can put token from cookie or other place you store.
      if (token) {
        $httpProvider.defaults.headers.common["Authorization"] = "Bearer " + token;
      }
    }])
`