DotNetSlackers: ASP.NET News for lazy Developers

Thursday, October 8, 2015

Claims Transformation and Authorization Policy in ASP.NET 5 (vNext)/MVC 6

        Introduction:

                 Sometimes claims inside a token of authenticated user may not be enough/sufficient for your application to decide whether the authenticated user is authorized or not. There are lot of reasons that you cannot put all claims inside the token (due to confidentiality, size-limitation, performance, etc.). For example, claims part of JWT token is just Base64 string encoded (so putting confidential/secret claims in JWT token does not make any sense). Cookie size limitation may be another reason for you. In this case, you may need to add/transform/remove/update claims by checking database/cache/file/other-token and/or you may need to set some default claims for guest/anonymous user. ASP.NET 5 makes these cases very simple to implement by allowing you to transform user's claims after authentication but before authorization. In this post, I will show you an example of claims transformation. I will also show you an example of authorization policy, a new feature of ASP.NET authorization which allows you to define code based policies, for example, you can define authorization policy that will only allow female sex users to execute some operation.   

        Description:

                    Note that I am using beta7 at the time of writing. Claims transformation is very easy to add in your application. First we need a class implementing IClaimsTransformer interface,

01public class MyClaimsTransformer : IClaimsTransformer
02{
03    public ClaimsPrincipal Transform(ClaimsPrincipal principal)
04    {
05        return principal;
06    }
07 
08    public virtual Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
09    {
10        if (principal.Identity.IsAuthenticated)
11        {
12            // get this from cache or db
13            var country = "India";
14            (principal.Identity as ClaimsIdentity).AddClaim(new Claim("Nationality", country));
15        }
16        return Task.FromResult(principal);
17    }
18}

                    Note that at the of writing IClaimsTransformer has only one TransformAsync method (and beta7 version only invoke TransformAsync method). The above method check whether the user is authenticated. If yes then the system will get the user nationality from db or cache and then add it to the user claims. Now we need to register this class in Startup.Configure method,

1app.UseClaimsTransformation(o => o.Transformer = new MyClaimsTransformer());

                    There is also ClaimsTransformer class in newer build that have OnTransform event which we can use rather than implementing IClaimsTransformer interface. Now let's add some authorization policy that will only allow peoples of Pakistan to execute Message action method,

1services.ConfigureAuthorization(options =>
2{
3    options.AddPolicy("MustBeIndian", policy => policy.RequireClaim("Nationality", "India"));
4});

1[Authorize(Policy = "MustBeIndian")]
2public IActionResult Message()
3{
4    return Content("Hi Indian");
5}


                    The above method will be only executed if authenticated user nationality is Pakistan. You can use the same example by only allowing male sex users to execute an action method. You can read more about authorization policies at https://github.com/aspnet/Announcements/issues/22.

No comments:

Post a Comment