gpt4 book ai didi

c# - 具有 System.Security.Claims 的 .NET Framework 中的复杂声明值

转载 作者:可可西里 更新时间:2023-11-01 09:07:21 27 4
gpt4 key购买 nike

我正在开发一个使用 Asp.Net 5 MVC、Owin 和 Oauth2 不记名 token 作为身份验证类型的网络应用。

this guide 添加自定义复杂声明 Json 并成功序列化到 Microsoft.IdentityModel.Claims.ClaimsIdentity 实例之后,我尝试使用 System.Security.Claims 命名空间上的 ClaimsIdentity 复制相同的示例。

不幸的是,似乎在complexClaim实例中添加了一个ClaimsIdentity,派生类的类型信息丢失了,声明存储为一个System.Security.Claims.Claim

var complexClaim = new ComplexClaim<UKPassport>(@"http://it.test/currentpassport", passport);
var claims = new List<Claim>() { complexClaim };
identity.AddClaims(claims);

当我尝试从身份取回声明时,将其强制转换为 ComplexClaim<UKPassport> 类型会导致空值。

var passportClaim = identity.Claims.FirstOrDefault<Claim>(c=>c.Type == @"http://it.test/currentpassport") as ComplexClaim<UKPassport>;

同一示例使用 Microsoft.IdentityModel.Claims 可以完美运行。

有什么提示吗?

这里是完整的移植代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Security.Claims;

namespace ConsoleApplication1
{
class Program {
private static ClaimsIdentity identity = new ClaimsIdentity();

static void Main(string[] args)
{
var oldPassport = CreatePassport();
identity.AddPassport(oldPassport);

var britishCitizen = identity.IsBritishCitizen();
var hasExpired = identity.IsCurrentPassportExpired();
Console.WriteLine(hasExpired);
Console.ReadLine();
}

private static UKPassport CreatePassport()
{
var passport = new UKPassport(
code: PassportCode.GBR,
number: 123456789,
expiryDate: DateTime.Now);

return passport;
}
}

public static class ClaimsIdentityExtensions {
public static void AddPassport(this ClaimsIdentity identity, UKPassport passport)
{
var complexClaim = new ComplexClaim<UKPassport>(@"http://it.test/currentpassport", passport);

var claims = new List<Claim>() { complexClaim };
identity.AddClaims(claims);
}

public static bool IsCurrentPassportExpired(this ClaimsIdentity identity)
{
var passport = GetPassport(identity, @"http://it.test/currentpassport");
return DateTime.Now > passport.ExpiryDate;
}

public static bool IsBritishCitizen(this ClaimsIdentity identity)
{
var passport = GetPassport(identity, @"http://it.test/currentpassport");
return passport.Code == PassportCode.GBR;
}

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

public enum PassportCode
{
GBR,

GBD,

GBO,

GBS,

GBP,

GBN
}


public class ComplexClaim<T> : Claim where T : ClaimValue
{
public ComplexClaim(string claimType, T claimValue)
: this(claimType, claimValue, string.Empty)
{
}

public ComplexClaim(string claimType, T claimValue, string issuer)
: this(claimType, claimValue, issuer, string.Empty)
{
}

public ComplexClaim(string claimType, T claimValue, string issuer, string originalIssuer)
: base(claimType, claimValue.ToString(), claimValue.ValueType(), issuer, originalIssuer)
{
}

public new T Value
{
get
{
return JsonConvert.DeserializeObject<T>(base.Value);
}
}
}

public class UKPassport : ClaimValue
{
public const string Name = "UKPassport";

private readonly PassportCode code;
private readonly int number;
private readonly DateTime expiryDate;

public UKPassport(PassportCode code, int number, DateTime expiryDate)
{
this.code = code;
this.number = number;
this.expiryDate = expiryDate;
}

public PassportCode Code { get { return this.code; } }
public int Number { get { return this.number; } }
public DateTime ExpiryDate { get { return this.expiryDate; } }

public override string ValueType()
{
return @"http://it.test/currentpassport";
}
}

public abstract class ClaimValue {
public abstract string ValueType();

public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
}

最佳答案

不支持也不推荐 - 声明是键/值对 - 尽可能简单。

.NET 中有许多支持类无法处理您想要实现的目标(SAM、CookieMiddleware 等)。

另请参阅此处 http://leastprivilege.com/2012/10/08/custom-claims-principals-in-net-4-5/

关于c# - 具有 System.Security.Claims 的 .NET Framework 中的复杂声明值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24813709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com