2017-07-17 15 views
1

コードベースはHttpContext.Userの値が評価されることが多いです。私はそれが値を割り当てられている単一の点を見ることができません。私はそれがAPIの深いところで起こると想定しています。HttpContext.Userはどのように値を取得しますか?

.NETはどのようにこの値を取得しますか?

+5

それはあなたが使用する認証メカニズムに依存します。あなたはどのようにユーザーを認証しますか? – Win

+0

これはHttpAuthを使用しますが、web.configは DSUK

答えて

1

それは、例えば、その後、HttpContext.Current.UserThread.CurrentPrincipalに割り当てられているIPrincipalを作成しています:

public class MyPrincipal : IPrincipal 
{ 
    public IIdentity Identity => new MyIdentity(); 

    public bool IsInRole(string role) 
    { 
     throw new NotImplementedException(); 
    } 
} 

public class MyIdentity : IIdentity 
{ 
    public string Name => throw new NotImplementedException(); 

    public string AuthenticationType => throw new NotImplementedException(); 

    public bool IsAuthenticated => throw new NotImplementedException(); 
} 

そして

var principal = new MyPrincipal(); 
HttpContext.Current.User = principal; 
Thread.CurrentPrincipal = HttpContext.Current.User; 

をこれが起こっている場所を正確には、あなたの認証メカニズムに依存します。

あなた自身で出荷したいので質問がある場合は、このような種類のトピックに熟練している場合にのみそれを実行してください。セキュリティは自明ではありません。

代わりに独自のものを作成するのではなく、ほとんどの状況では、アイデンティティ(IUserStoreなど)を拡張した方が良いかもしれません:

https://docs.microsoft.com/en-us/aspnet/identity/overview/extensibility/overview-of-custom-storage-providers-for-aspnet-identity

関連する問題