简体   繁体   English

使用AngularJs,WebAPI和OAuth2时,获得带有JWT的明文用户角色

[英]Get user role in clear text along with JWT when using AngularJs, WebAPI and OAuth2

I am sing OAuth2 in WebAPI project. 我在WebAPI项目中唱OAuth2。 I am authenticating user request in OWIN middleware. 我正在OWIN中间件中验证用户请求。 On successfull authentication I am sending an JWT access token to client. 身份验证成功后,我将向客户端发送JWT访问令牌。 Now I can validate subsequent request at server and use [Authorize(Roles="myRole")] attribute on Api Controllers. 现在,我可以在服务器上验证后续请求,并在Api控制器上使用[Authorize(Roles =“ myRole”)]属性。

But how can I show validate client content in AngularJs and show pages based on user role? 但是,如何在AngularJs中显示验证客户端内容并根据用户角色显示页面? I have JWT at client and no idea how to get user role out of it? 我在客户端有JWT,不知道如何从中获得用户角色?

Is it a good approach to extract information from JWT? 从JWT提取信息是一种好方法吗?

You will need to parse that JWT and get the values out. 您将需要解析该JWT并获取值。 You can do that with the help of the angular-jtw library. 您可以借助angular-jtw库来做到这一点。

1) Download the angular-jwt.min.js ( https://github.com/auth0/angular-jwt ) 1)下载angular-jwt.min.js( https://github.com/auth0/angular-jwt

2) put a dependecy of "angular-jwt" on the application module: 2)在应用程序模块上放置“ angular-jwt”的依赖项:

var app = angular.module("YOUR_APP", ["angular-jwt"]);

3) pass the jwtHelper to your service or controller or wherever it is that you wish to use it. 3)将jwtHelper传递给您的服务或控制器,或者将其传递到您希望使用它的任何地方。

    app.module.factory("YOUR_SERVICE", function(jwtHelper){
    ...
});

4) use the decodeToken method of the jwtHelper you passed in to decode your token 4)使用传入的jwtHelper的encodeToken方法解码令牌

For example, the code below is parsing out the role object from a jwt that came back from my service endpoint. 例如,下面的代码从服务端点返回的jwt中解析角色对象。 Upon succssful return from the server the role is extracted from the jwt and returned. 从服务器成功返回后,将从jwt中提取角色并返回。

return $http.post(serviceEndPoints.tokenUrl, data, config)
                    .then(function (response) {
                        var tokenPayLoad = jwtHelper.decodeToken(response.data.access_token);

//Now do whatever you wish with the value. Below I am passing it to a function: (determineRole)

                        var userRole = determineRoles(tokenPayLoad.role);


            return userRole;
        });
    };

Hope that helps 希望能有所帮助

//Houdini //霍迪尼

Currently we don't offer anything that would help you to take advantage of that information on the client. 目前,我们没有提供任何可帮助您在客户端上利用这些信息的东西。 Also note: as today we do not validate the token on the client, we cannot really trust its content... while the [Authorize] attribute on the server side gets the role info only after the pipeline before it had a chance of validating the signature and deciding that the token is valid. 另请注意:由于今天我们不在客户端上验证令牌,因此我们无法真正信任其内容...而服务器端的[Authorize]属性仅在管道有机会验证令牌之前才获得角色信息。签名并确定令牌有效。 We might introduce something that will help with this scenario in the future, but for the time being you'd need to write custom code or rely on the server side to echo things back. 我们可能会在将来引入一些对这种情况有帮助的内容,但暂时您需要编写自定义代码或依靠服务器端将内容回显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM