Complex Claim Values in .NET Framework with System.Security.Claims

前端 未结 2 1649
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 12:47

I\'m developing a web app with Asp.Net 5 MVC, Owin and Oauth2 bearer token as auth type.

Following this guide that adds a custom complex claim Json serialized to an inst

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-08 13:19

    The cast in GetPassport tries to convert from base type Claim to derived type ComplexClaim which will result in null. You need to write a cast operator to convert from Claim to UKPassport

    public static explicit operator UKPassport(Claim c)
    {
         return (c == null ? null:JsonConvert.DeserializeObject (c.Value));
    }       
    

    and GetPassport will be

    private static UKPassport GetPassport(this ClaimsIdentity identity, string passportType)
    {
       return (UKPassport)identity.Claims.FirstOrDefault(c => c.Type == @"http://it.test/currentpassport");
    }
    

提交回复
热议问题