문제

이것은 기본적인 질문처럼 보일 수 있으며 HTTP 프로토콜 101로 돌아갑니다. 그러나 기본 인증이 어떻게 작동하는지 이해하는 데 어려움이 있습니다. Windows 서비스를 구현하고 있으며 안전해야합니다. 사용자 이름과 비밀번호를 얻고 사용자를 사용자 정의 사용자 저장소로 인증하고 싶습니다. 로그인 호출이 SQL 서버에 대한 호출을 나타내므로 로그인 호출 수를 최소화하고 싶습니다. 내가 지금까지 시작한 것은 다음과 같습니다.

USERUSTORIZED 기능을 보는 방식은 사용자 정의 로그인을 호출해야합니다. 그러나 나는 매번 그렇게하고 싶지 않습니다. 로그인 한 경우 기본 인증이 유지되거나 탐색해야 할 캐싱 스레드 안전 솔루션이 있습니까?

오 예, 사용자가 인증을 받고 리스너가 사용자/연결을위한 후속 콜백을 참조 할 수 있도록 스레드에서 객체가 생성되고 유지되도록 만들고 싶습니다. 그러나 ListenerCallback은 정적이기 때문에 이것이 어떻게 달성 될 수 있는지 잘 모르겠습니다.

도움을 주셔서 감사합니다. 진심으로 감사합니다.

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 문서만으로는 인증 체계의 Caparison과 그들이 어떻게 작동하는지에 대한 눈을 뜨고 있습니다. 내가 이것을 잘못 준비하지 않는 한, 다이제스트 체계는 "세션"또는 적어도 내 사용자 지정 인증을 재 시도하기위한 만료를 유지할 수있는 것으로 보입니다.

도움이 되었습니까?

해결책 2

사양에 감사드립니다. 감사합니다. 나는 내 문제를 해결했으며 사양 관련이 아니라 디자인/호스팅 문제였습니다.

다른 팁

HTTP 기본 모든 요청마다 로그인 자격 증명이 필요합니다. HTTP에는 세션 개념이 없으므로 누군가가 "이미 로그온 한"인지 실제로 알 수 없습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top