----------box程---------- ----------end---------- ----------box程---------- Bob Abracadabra user1 QSMAKo67+vzYnU9TcMSqOFXy14U= ----------end---------- ----------box程---------- USE NORTHWIND GO ALTER TABLE [dbo].[Employees] ADD [Username] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Password] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [roles] [varchar] (250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL GO INSERT INTO EMPLOYEES (Firstname, Lastname,Username, [Password], roles) VALUES('User','One', 'user1', 'pass1', 'user') GO ----------end---------- ----------box程---------- ----------end---------- ----------box程---------- namespace WSESecurity { public class WSEPasswordProvider : IPasswordProvider { public string GetPassword(UsernameToken token) { try { SqlConnection cn = new        SqlConnection(System.Configuration.ConfigurationSettings.  AppSettings["SqlConn"].ToString()); cn.Open(); SqlCommand cmd = new SqlCommand("SELECT Username, password from Employees where username ='" + token.Username + "'",cn); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); dr.Read(); return dr["password"].ToString();       } catch(Exception ex) { throw new Exception (ex.Message); }  } }  } ----------end---------- ----------box程---------- [WebMethod] public DataSet CustOrderHist(string CustId) { // 只接受SOAP格式的要求 SoapContext requestContext = HttpSoapContext.RequestContext; if(requestContext==null) { throw new ApplicationException("Non-SOAP request!"); } // 確認所有Tokens集合中的SecurityToken bool valid=false; try { foreach(SecurityToken tkn in requestContext.Security.Tokens) { if(tkn is UsernameToken) valid=true; } } catch(Exception ex) { throw new Exception( ex.Message + ": " + ex.InnerException.Message); } if (valid==false) throw new ApplicationException("Invalid or Missing Security Token."); SqlConnection cn; SqlDataAdapter da; DataSet ds; cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlConn"].ToString()); cn.Open(); da = new SqlDataAdapter("custorderHist '" +CustId + "'", cn); ds = new DataSet(); da.Fill(ds, "CustOrderHist"); return ds; } ----------end---------- ----------box程---------- private void Button1_Click(object sender, System.EventArgs e) { localhost.SecurityServiceWse wse=new localhost.SecurityServiceWse(); UsernameToken tkn = new UsernameToken(txtUsername.Text,txtPassword.Text,PasswordOption.SendHashed); wse.RequestSoapContext.Security.Tokens.Add (tkn); try { DataSet ds=wse.CustOrderHist(txtCustID.Text); DataGrid1.DataSource=ds; DataGrid1.DataBind(); } catch(Exception ex) { DataGrid1.Visible=false; lblMessages.Text=ex.Message; } } ----------end---------- ----------box程---------- C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\certmgr.exe ----------end---------- ----------box程---------- makecert [options] outputCertificateFile ----------end---------- ----------box程---------- makecert testCert.cer ----------end---------- ----------box程---------- makecert -sk PAB -n "CN=PeterBromberg" -ss root -sr localmachine testPAB.cer ----------end---------- ----------box程---------- private X509CertificateStore store; private void button1_Click(object sender, System.EventArgs e) { store = X509CertificateStore.CurrentUserStore( X509CertificateStore.RootStore.ToString()); store.OpenRead(); foreach(X509Certificate cert in store.Certificates) { listBox1.Items.Add(cert.GetName()); } store =X509CertificateStore.LocalMachineStore(X509CertificateStore.RootStore.ToString()); store.OpenRead(); foreach(X509Certificate cert in store.Certificates) { listBox1.Items.Add(cert.GetName()); } } 你也可以使用搜尋的方法來取得認證,如下所示: X509CertificateCollection cc=store.FindCertificateBySubjectString(strName); foreach(X509Certificate cert3 in cc) listBox1.Items.Add(cert3.GetName()); ----------end---------- ----------box程---------- C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys ----------end---------- ----------box程---------- ----------end---------- ----------box程---------- using Microsoft.Web.Services.Security.X509; ----------end---------- ----------box程---------- [WebMethod] public DataSet CustOrderHist(string CustId) { // 只接受SOAP格式要求 SoapContext requestContext = HttpSoapContext.RequestContext; if(requestContext==null) { throw new ApplicationException("不是SOAP要求!"); } // 確認所有Tokens集合中的SecurityToken bool valid=false; try { foreach(SecurityToken tkn in requestContext.Security.Tokens) { if(tkn is X509SecurityToken) valid=true; } } catch(Exception ex) { throw new Exception( ex.Message + ": " + ex.InnerException.Message); } if (valid==false) throw new ApplicationException("Invalid Credentials."); SqlConnection cn; SqlDataAdapter da; DataSet ds ; cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlConn"].ToString()); cn.Open(); da = new SqlDataAdapter("custorderHist '" +CustId + "'", cn); ds = new DataSet(); da.Fill(ds, "CustOrderHist"); return ds; } ----------end---------- ----------box程---------- private void Button1_Click(object sender, System.EventArgs e) { store = X509CertificateStore.CurrentUserStore( X509CertificateStore.RootStore.ToString()); store.OpenRead(); localhost.CertificateServiceWse wse=new localhost.CertificateServiceWse(); X509CertificateCollection col=(X509CertificateCollection)store.FindCertificateBySubjectString(txtCertificate.Text); X509Certificate cert =null; try { cert = col[0]; } catch(Exception ex) { lblMessages.Text="Certificate not Found!"; return; } wse.RequestSoapContext.Security.Tokens.Add (new X509SecurityToken(cert)); try { DataSet ds=wse.CustOrderHist(txtCustID.Text); DataGrid1.DataSource=ds; DataGrid1.DataBind(); } catch(Exception ex) { DataGrid1.Visible=false; lblMessages.Text=ex.Message; } } ----------end---------- ----------box程---------- http://tempuri.org/CustOrderHist http://localhost/WSECertificateAuth/CertificateService.asmx uuid:e4992608-7930-434c-9a54-0453ac189d0d 2002-12-31T15:36:34Z 2002-12-31T15:41:34Z MIIBcTCCARugAwIBAgIQLIB/4r0Rf4RL7upb3E2lAzANBgkqhkiG9w0BAQQFADA WMRQwEgYDVQQDEwtSb290IEFnZW5jeTAeFw0wMjEyMzAyMDQyMjVaFw0zOTEyMz EyMzU5NTlaMBAxDjAMBgNVBAMTBVBldGV5MFwwDQYJKoZIhvcNAQEBBQADSwAwS AJBAMDRte7rxIpqBT0SYSXpw7773Ex0fiUfzFapAxCh4O2PQctO2UiiM4xzA/UZ qfo08rUZLltT3XPWOEMxwKxrxmsCAwEAAaNLMEkwRwYDVR0BBEAwPoAQEuQJLQY dHU8AjWEh3BZkY6EYMBYxFDASBgNVBAMTC1Jvb3QgQWdlbmN5ghAGN2wAqgBkih HPuNSqXDX0MA0GCSqGSIb3DQEBBAUAA0EAEKQ23JTWrFCUdmck/CUkv8ruAgEyU BOo14RkWRSiLfT17zf4zKDGuO0jJRZHBNsDhfUeWjy/9e4d8G5czgTpgA== ----------end---------- ----------box程---------- wse.RequestSoapContext.Security.Tokens.Add (new X509SecurityToken(cert)); X509SecurityToken crtTkn = new X509SecurityToken(cert); wse.RequestSoapContext.Security.Tokens.Add(crtTkn); wse.RequestSoapContext.Security.Elements.Add(new Signature(crtTkn)); ----------end---------- ----------box程---------- public DataSet CustOrderHist(string CustId) { // 只接受SOAP格式要求 SoapContext requestContext = HttpSoapContext.RequestContext; if(requestContext==null) { throw new ApplicationException("Non-SOAP request!"); } //確認所有Tokens集合中的SecurityToken bool valid=false; try { foreach(SecurityToken tkn in requestContext.Security.Tokens) { if(tkn is X509SecurityToken) { foreach(Object elem in requestContext.Security.Elements) { if(elem is Signature) { Signature sign=(Signature)elem; // 驗證要求的本體簽章--- if(sign!=null && (sign.SignatureOptions & SignatureOptions.IncludeSoapBody)!=0) { if(sign.SecurityToken is X509SecurityToken) valid=true; } } } } } } catch(Exception ex) { throw new Exception( ex.Message + ": " + ex.InnerException.Message); } if (valid==false) throw new ApplicationException("Invalid Credentials."); SqlConnection cn; SqlDataAdapter da; DataSet ds ; cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlConn"].ToString()); cn.Open(); da = new SqlDataAdapter("custorderHist '" +CustId + "'", cn); ds = new DataSet(); da.Fill(ds, "CustOrderHist"); return ds; } } ----------end----------