firefox浏览器认证

在firefox浏览器的认证过程是在netwerk/protocol/http目录下的nsHttpBasicAuth.cpp文件中实现的:

NS_IMETHODIMP
nsHttpBasicAuth::GenerateCredentials(nsIHttpAuthenticableChannel *authChannel,
   const char *challenge,
   bool isProxyAuth,
   const PRUnichar *domain,
   const PRUnichar *user,
   const PRUnichar *password,
   nsISupports **sessionState,
   nsISupports **continuationState,
   uint32_t *aFlags,
   char **creds)

{
   LOG(("nsHttpBasicAuth::GenerateCredentials [challenge=%s]\n", challenge));
   
   NS_ENSURE_ARG_POINTER(creds);

   *aFlags = 0;

   // we only know how to deal with Basic auth for http.
   bool isBasicAuth = !PL_strncasecmp(challenge, "basic", 5);
   NS_ENSURE_TRUE(isBasicAuth, NS_ERROR_UNEXPECTED);

   // we work with ASCII around here
   nsAutoCString userpass;
   LossyCopyUTF16toASCII(user, userpass);
   userpass.Append(':'); // always send a ':' (see bug 129565)
   if (password)
   LossyAppendUTF16toASCII(password, userpass);

   // plbase64.h provides this worst-case output buffer size calculation.
   // use calloc, since PL_Base64Encode does not null terminate.
   *creds = (char *) calloc(6 + ((userpass.Length() + 2)/3)*4 + 1, 1);
   if (!*creds)
   return NS_ERROR_OUT_OF_MEMORY;

   memcpy(*creds, "Basic ", 6);
   PL_Base64Encode(userpass.get(), userpass.Length(), *creds + 6);
   
   return NS_OK;
}

通过PL_Base64Encode(userpass.get(), userpass.Length(), *creds + 6)函数去获取浏览器中设置的用户名和密码信息,userpass.get()表示用户名和密码信息,中间用:号隔开,而userpass.Length()表示userpass.get()的字符长度。因此,要想给firefox浏览器添加默认的认证信息,只要对这个函数进行修改则可,例如:
PL_Base64Encode(“abc:123”, 7, *creds + 6)则将用户名默认为abc,密码默认为123。

在firefox浏览器的认证过程中,密码的长度一般不能超过8位,当期长度超过8位时,可能无法认证成功。

认证过程,领悟到了:3_47:火狐浏览器比IE浏览器较为严格,它的容错能力较低,但IE浏览器不一样,它服务器写了很多容错的。所以写网页时,IE也许显示证确,但火狐浏览器不一定!