⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 authuser.cpp

📁 Visual C++ From the Ground Up Second Edition 例程源代码
💻 CPP
字号:
// AUTHUSER.CPP - Implementation file for your Internet Server
//    AuthUser Filter

#include "stdafx.h"
#include "AuthUser.h"

///////////////////////////////////////////////////////////////////////
// The one and only CWinApp object
// NOTE: You may remove this object if you alter your project to no
// longer use MFC in a DLL.

CWinApp theApp;



///////////////////////////////////////////////////////////////////////
// The one and only CAuthUserFilter object

CAuthUserFilter theFilter;


///////////////////////////////////////////////////////////////////////
// CAuthUserFilter implementation

CAuthUserFilter::CAuthUserFilter()
{
}

CAuthUserFilter::~CAuthUserFilter()
{
}

BOOL CAuthUserFilter::GetFilterVersion(PHTTP_FILTER_VERSION pVer)
{
	// Call default implementation for initialization
	CHttpFilter::GetFilterVersion(pVer);

	// Clear the flags set by base class
	pVer->dwFlags &= ~SF_NOTIFY_ORDER_MASK;

	// Set the flags we are interested in
	pVer->dwFlags |= SF_NOTIFY_ORDER_LOW | SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT
			 | SF_NOTIFY_AUTHENTICATION;

	// Load description string
	TCHAR sz[SF_MAX_FILTER_DESC_LEN+1];
	ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
			IDS_FILTER, sz, SF_MAX_FILTER_DESC_LEN));
	_tcscpy(pVer->lpszFilterDesc, sz);
	return TRUE;
}

DWORD CAuthUserFilter::OnAuthentication(CHttpFilterContext* pCtxt,
	PHTTP_FILTER_AUTHENT pAuthent)
{
	CString	oBuffer;		// Buffer for client output.
	DWORD	dwSize;			// Size of the buffer.
	LPVOID	pvInOut;		// Client input or output.

	// See if we're getting an anonymous request.
	if (strlen(pAuthent->pszUser) == 0)
	{

		// Get the user's name.
		dwSize = 100;
		pvInOut = " ";
		pCtxt->GetServerVariable("REMOTE_USER", pvInOut, &dwSize);
		dwSize = strlen(LPTSTR(pvInOut));

		if (strncmp(LPTSTR(pvInOut), " ", 1) == 0)
		{

			// Set an error condition.
			pCtxt->ServerSupportFunction(SF_REQ_SEND_RESPONSE_HEADER, 
				"401 Access Denied", 
				NULL, 
				NULL);
		}
		else
		{

			// Indicate the user has supplied a password.
			pCtxt->ServerSupportFunction(SF_REQ_SEND_RESPONSE_HEADER, 
				NULL, 
				NULL, 
				NULL);

			// Store the user's name.
			strncpy(pAuthent->pszUser, LPTSTR(pvInOut), dwSize);

			// Get and store the user's password.
			dwSize = 100;
			pvInOut = " ";
			pCtxt->GetServerVariable("AUTH_PASS", pvInOut, &dwSize);
			dwSize = strlen(LPTSTR(pvInOut));
			strncpy(pAuthent->pszPassword, LPTSTR(pvInOut), dwSize);
		}
	}

	// return the appropriate status code
	return SF_STATUS_REQ_NEXT_NOTIFICATION;
}

// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CAuthUserFilter, CHttpFilter)
	//{{AFX_MSG_MAP(CAuthUserFilter)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

///////////////////////////////////////////////////////////////////////
// If your extension will not use MFC, you'll need this code to make
// sure the extension objects can find the resource handle for the
// module.  If you convert your extension to not be dependent on MFC,
// remove the comments arounn the following AfxGetResourceHandle()
// and DllMain() functions, as well as the g_hInstance global.

/****

static HINSTANCE g_hInstance;

HINSTANCE AFXISAPI AfxGetResourceHandle()
{
	return g_hInstance;
}

BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
					LPVOID lpReserved)
{
	if (ulReason == DLL_PROCESS_ATTACH)
	{
		g_hInstance = hInst;
	}

	return TRUE;
}

****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -