0

これは、HTTP プロトコル 101 に戻る基本的な質問のように思えるかもしれません。しかし、基本認証がどのように機能するかを理解するのに苦労しています。Windows サービスを実装しており、セキュリティで保護する必要があります。ユーザー名とパスワードを取得し、カスタム ユーザー ストアに対してユーザーを認証したいと考えています。また、ログイン呼び出しは SQL サーバーへの呼び出しを表すため、ログイン呼び出しの数を最小限に抑えたいと考えています。私がこれまでに始めたことは、次のようなものです。

私の見方では、UserAuthorized 関数がカスタム ログイン呼び出しを行う必要があります。しかし、私は毎回それをする必要はありません。ログインしている場合、基本認証は維持されますか、それとも私が調査すべきキャッシング スレッド セーフ ソリューションはありますか。

そうそう、ユーザーが認証されたときにオブジェクトが作成され、リスナーがユーザー/接続の後続のコールバックで参照できるようにスレッドで維持されるようにしたいと思います。しかし、ListenerCallback は静的であるため、これをどのように実現できるかわかりません。

助けてくれてありがとう、私はそれとStackOverflowに本当に感謝しています。

public void ThreadProc() {
    string uriPrefix = ConfigurationManager.AppSettings["ListenerPrefix"];
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add(uriPrefix);
    listener.AuthenticationSchemes = AuthenticationSchemes.Basic;

    listener.Start();

    Console.WriteLine("Start listening on " + uriPrefix);
    Console.WriteLine("Press Control-C to stop listener...");

    while (listening) {
        IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
        result.AsyncWaitHandle.WaitOne();
    }
}

public static void ListenerCallback(IAsyncResult result) {
    HttpListener listener = (HttpListener)result.AsyncState;
    HttpListenerContext context = listener.EndGetContext(result);
    WebDavEngine engine = new WebDavEngine(context.User);

    context.Response.SendChunked = false;
    FileLogger.Level = LogLevel.Debug;

    engine.IgnoreExceptions = false;

    if (UserAutorized(context)) {
        try {
            engine.Run(context, listener.Prefixes);
            engine.CommitTransaction();
        } catch {
            engine.RollBackTransaction();
        } finally {
            engine.CloseConnection();
        }
    } else
        context.Response.StatusCode = 401;

    if (context.Response.StatusCode == 401)
        ShowLoginDialog(context, context.Response);

    try {
        context.Response.Close();
    } catch {
        // client closed connection before the content was sent
    }
}

private static bool UserAutorized(HttpListenerContext context) {
    HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;

    if (!identity.IsAuthenticated) {
        string username = identity.Name;

        // workaround for Windows Vista Basic authentication. User name may be submitted in the following format: Machine\User.
        int ind = username.LastIndexOf('\\');
        if (ind > 0)
            username = username.Remove(0, ind + 1);


        Console.WriteLine("Trying Authentication since identity is NOT authenticated");

        return false;
    } else {
        Console.WriteLine("identity.IsAuthenticated: " + identity.IsAuthenticated.ToString());
        return identity.IsAuthenticated;
    }            
}

編集:ドキュメントだけで+1。認証スキームの機能とその仕組みに本当に目を向けました。私がこれを間違って準備していない限り、ダイジェストスキームは「セッション」または少なくとも有効期限を維持して、カスタム認証を再試行できるように見えます。

4

2 に答える 2

4

HTTP Basicでは、すべての要求でログイン資格情報が必要です。HTTP にはセッションの概念がないため、誰かが「既にログオンしている」かどうかを実際に判断することはできません。

于 2009-02-20T17:52:17.717 に答える
0

仕様をありがとう、私はそれを感謝しました。問題を解決することができましたが、仕様に関連するものではなく、設計/ホスティングの問題でした。

于 2009-03-25T06:22:16.537 に答える