FBA thiab SQL neeg rau zaub mov: Zaj dabneeg hlub

Kuv colleague tau ua haujlwm rau ib qho web nyob hauv ib lub chaw kawm FBA. Cov yam, cov Web site qhov cia li nkaum kiag tej cov ntaub ntawv los ntawm cov neeg rau zaub mov SQL. Lub hom phiaj grand During dictates tias ib cov DBA configures ntawv theem ruaj ntseg hauv SQL (as opposed to embedding ib tug neeg ID rau ib SQL cov lus nug los yog ib txhia lwm mus kom ze).

Qhov teeb meem yog tias SQL neeg rau zaub mov tsis paub dab tsi txog peb lub chaw FBA ces nws yuav tsis ntseeg peb. Solved tej teeb meem no yog peb, for lack of muaj ib lo lus zoo, manually impersonating tus neeg siv AD uas peb muaj txuas rau SQL xws tias SQL cov theem tej hauj lwm ruaj ntseg.

Txawm tias FBA yog ib qho kev feature ASP.NET, peb teb chaws SharePoint neeg tau qhia ntau yam tshawb engines uas yog koj nyob nraum querying FBA, koj yuav tsum txhais hais tias koj xav paub yuav ua li cas rau configure FBA nyob SharePoint. Kuv ua tsis tau tejyam rau nrhiav tej ntaub ntawv thiaj li yuav pab tau ib FBA oriented ASP.NET daim ntawv thov mus sib tham nrog SQL nyob rau hauv txoj kev uas peb yuav tsum tau xyuas.

Nyuam qhuav pib researching no, peb rov nyeem no Tshooj: ASP.NET Impersonation

Tshawb fawb coj peb mus no codproject Tshooj: http://www.codeproject.com/KB/cs/cpimpersonation1.aspx

Uas pab peb sau ntawv peb kev cai, uas kuv twb muaj nyob hauv qab. Nws yuav tsis tau cov khoom feem ntau elegant, tab sis nws ua haujlwm. Kuv vam tias koj pom tias nws yuav pab tau.

Ntawm no yog cov chaws uas ua haujlwm rau peb:

kev tiv thaiv btnSearchCarrier_Click tsis muaj dabtsis(nruas sender, EventArgs e)
 {
 ua tiag
 {
 ImpersonateUser xyuas = tshiab ImpersonateUser();
 // TODO: Muab tau peev xwm iU.Impersonate("DomainName", "UserName", "Password");

//
 CHAWS
//

 iU.Undo();
 }
 ntes (Kos ex)
 {

 }
 }

// Siv Impersonation chav kawm ntawv raws li hais hauv qab no.

pej xeem hoob ImpersonateUser
 {
 [DllImport("advapi32.dll", SetLastError = tseeb)]
 pej xeem zoo li qub extern bool LogonUser(
 Txoj hlua lpszUsername,
 Txoj hlua lpszDomain,
 Txoj hlua lpszPassword,
 rau cov menyuam dwLogonType,
 rau cov menyuam dwLogonProvider,
 nyob IntPtr phToken);

 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 lwm yam extern zoo li qub bool CloseHandle(IntPtr kov);

 lwm yam zoo li qub IntPtr tokenHandle = tshiab IntPtr(0);
 lwm yam zoo li qub WindowsImpersonationContext impersonatedUser;

 // Yog hais tias koj teeb cov code no rau ib DLL, Nco ntsoov demand ntawd nws
 // sau nrog FullTrust.
 [PermissionSetAttribute(SecurityAction.Demand, Lub npe = "FullTrust")]
 pej xeem Impersonate tsis muaj dabtsis(txoj hlua domainName, txoj hlua userName, txoj hlua password)
 {
 ua tiag
 {

 // Siv cov unmanaged LogonUser kev ua kom tau tus neeg siv token rau
 // tus neeg siv kev cai tswjhwm, sau, thiab lo lus zais.
 const rau cov menyuam LOGON32_PROVIDER_DEFAULT = 0;

 // Dua no parameter ua LogonUser los tsim tau ib cov thawj token.
 const rau cov menyuam LOGON32_LOGON_INTERACTIVE = 2;
 tokenHandle = IntPtr.Zero;

 // Kauj ruam -1 Hu rau LogonUser mus nrhiav ib tug kov rau qhov kev mus saib token.
 bool returnValue = LogonUser(
 userName,
 domainName,
 lo lus zais,
 LOGON32_LOGON_INTERACTIVE,
 LOGON32_PROVIDER_DEFAULT,
 nyob tokenHandle); // tokenHandle - ruaj ntseg token tshiab

 Yog hais tias (== returnValue tsis tseeb)
 {
 rau cov menyuam ret = Marshal.GetLastWin32Error();
 Console.WriteLine("LogonUser hu ua tsis tau tejyam uas yuam kev cai : " +
 ret);
 pov System.ComponentModel tshiab.Win32Exception(ret);
 }

 // Kauj ruam - 2
 WindowsIdentity newId = tshiab WindowsIdentity(tokenHandle);
 // Kauj ruam -3
 impersonatedUser = newId.Impersonate();

 }
 ntes (Kos ex)
 {
 Console.WriteLine("Kos tshwm sim. " + ex.Xov);
 }
 }


 /// <txoj kev>
 /// Nres impersonation
 /// </txoj kev>
 pej xeem Undo tsis muaj dabtsis()
 {
 impersonatedUser.Undo();
 // Dawb lub tokens.
 Yog hais tias (tokenHandle != IntPtr.Zero)
 CloseHandle(tokenHandle);
 }
 }

</kawg>

Subscribe rau kuv blog.

Ntawv cim npe Technorati:

Ib tug xav rau"FBA thiab SQL neeg rau zaub mov: Zaj dabneeg hlub

  1. Da-viv ntxov
    Thov txim rau xo no ncaj cov lus nug tab sis kuv kuj tsis paub tseeb xa tuaj rau koj tus email ntawm no blog yuav ua li cas.
    Koj muaj ib tsev xa tas los uas koj muaj ib txoj kev zoo mus lim lub tsev qiv ntawv los saib tau cov ntaub ntawv untagged. Koj puas muaj yam xav hais li cas koj yuav saib untagged ntaub ntawv uas nyob rau hauv ib lub tsev qiv ntawv uas yog tas nrho cov ntaub ntawv? Siv ib daim teb tas nyob txawm nrhiav tsis rov qab muaj ntsiab.
    Teb

sau ntawv cia

Koj email chaw nyob yuav tsis muab luam tawm. Search cim *