DECLARE @Tuan INT
, @Nam char(4);
SELECT @Tuan = 38
, @Nam = 2014
-- once you have the @WeekNum and @YearNum set, the following calculates the date range.
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @Nam ) + (@Tuan -1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @Nam ) + (@Tuan -1), 5) AS EndOfWeek;
raising values
Thứ Sáu, 17 tháng 10, 2014
Thứ Năm, 9 tháng 10, 2014
Thực thi một thủ tục trên SQL server 2008 - execute procedure SQL server
1. Tạo DB có tên là DBTest
2. Tạo một bảng User trong DBTest
create table User(UserID int primary key identity(1,1), UserName varchar(50));
insert into User Values('test')
insert into User Values('test2')
3. Nếu chưa có thủ tục ->Tạo thủ tục. Câu lệnh tạo thủ tục
CREATE PROC test(@UserID int)--@UserID là tham số truyền vào kiểu số nguyên
AS
BEGIN
select Username from User where UserID=@UserID;
END
4. Câu lệnh thực thi
execute test @UserID=1;
5.Kết quả
-- UserName
test
2. Tạo một bảng User trong DBTest
create table User(UserID int primary key identity(1,1), UserName varchar(50));
insert into User Values('test')
insert into User Values('test2')
3. Nếu chưa có thủ tục ->Tạo thủ tục. Câu lệnh tạo thủ tục
CREATE PROC test(@UserID int)--@UserID là tham số truyền vào kiểu số nguyên
AS
BEGIN
select Username from User where UserID=@UserID;
END
4. Câu lệnh thực thi
execute test @UserID=1;
5.Kết quả
-- UserName
test
Thứ Tư, 8 tháng 10, 2014
Tính số ngày giữa 2 ngày tháng cho trước C#
DateTime now = DateTime.Now;
DateTime then = new DateTime (1, 1, 2005);
TimeSpan diff = now - then;
int days = diff.Days;
Hoặc.
DateTime then = new DateTime (1, 1, 2005);
TimeSpan diff = now - then;
int days = diff.Days;
Hoặc.
private int DateDiffInDays(DateTime fromDate, DateTime toDate)
{
return toDate.Subtract(fromDate).Days;
}
Thứ Bảy, 4 tháng 10, 2014
Tính tổng số phút tới thời điểm hiện của ngày hiện tại trong tuần C#
DateTime.Now.TimeOfDay.TotalMinutes
Thứ Năm, 2 tháng 10, 2014
Configuring Apache for SSL and LDAP authentication
Configuring Apache for SSL and LDAP authentication
Imagine you have a directory on your web server you want to protect so that people have to login to see the item in it. You don’t want to give them a different password to remember but use an existing username/password in an LDAP directory. You want to make sure that when they login that their username and password are sent encrypted across the network. Below are directions to do exactly what I describe. That is if you have an LDAP server.
The Head of Computer and Networking Systems originally wrote these directions (which are amazing and better than most of what I found on the web) when I wanted to password protect our internal blogs. I’ve updated them to be more generic and posted them because they are relevant if you want to set up LDAP authentication for MediaWiki. Note that steps f to i in the section “Configure LDAPs authentication for Directory access”
Set up SSL on Web Server
- On the server where you want to perform SSL, Generate Server Private key (one time)
- Remove default keys
cd /etc/apache2/
rm ssl.key/server.key
rm ssl.crt/server.crt - Generate new private key
/usr/bin/openssl genrsa 1024 > /etc/apache2/ssl.key/server.key - Set secure permissions on key file
chmod go-rwx /etc/apache2/ssl.key/server.key
- Remove default keys
- Create self-signed certificate
- Create certificate
/usr/bin/openssl req -new -key /etc/apache2/ssl.key/server.key -x509 -days 365 -out
/etc/apache2/ssl.crt/server.crtYou are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]: State
Locality Name (eg, city) []: City
Organization Name (eg, company) [Internet Widgits Pty Ltd]: Some Organization
Organizational Unit Name (eg, section) []: Some Unit
Common Name (eg, YOUR name) []: server.webaddress.com
Email Address []: email@address.com
- Create certificate
- Configure apache for SSL
- Tell apache SSL port
- Edit /etc/apache2/listen.conf and remove # before:
Listen 443 - Copy virtual host template file (must end in .conf)
cp /etc/apache2/vhosts.d/vhost-ssl.template /etc/apache2/vhosts.d/vhost-ssl.conf - Change the following lines in /etc/apache2/vhosts.d/vhost-ssl.conf#<IfDefine SSL>
#<IfDefine !NOSSL><VirtualHost server.webaddress.com:443>DocumentRoot “/srv/www/htdocs”
ServerName server.webaddress.com:443
ServerAdmin email@address.com
ErrorLog /var/log/apache2/error_443_log
TransferLog /var/log/apache2/access_443_log
#</IfDefine>
#</IfDefine>
- Edit /etc/apache2/listen.conf and remove # before:
- Restart Apache with new configuration
Apache2ctl stop
Apache2ctl start - Use Netstat to check to see if httpd is listening on port 443
netstat -anp |more - Open port on Firewall for 443
- Tell apache SSL port
- Test in web browser https://server.webaddress.com
Configure LDAPs authentication for Directory access
- Import certificate for ldap server into the web server where you want to authenticate via LDAP
- Export server certificate from your LDAP server
- Open exported certificate file and copy text
- Create file /etc/apache2/server-name.crt
- Paste certificate text into file
- Edit /etc/apache2/default-server.conf and add following lines:LDAPTrustedCAType BASE64_FILE
LDAP
TrustedCA /etc/apache2/server-name.crt - Edit /etc/apache2/vhosts.d/vhost-ssl.conf
- Add lines following between <VirtualHost> and </VirtualHost>
<Directory /srv/www/htdocs/directory_you_want_to_protect_with_ldap>
AuthType Basic
AuthName “LDAPs Login”
AuthLDAPEnabled onAuthLDAPURL “ldaps://ldapserver.domain.edu:ldapport#/OU=usergroup,DC=subdomain,DC=mydomain,DC=edu?sAMAccountName?sub?(objectClass=*)”
AuthLDAPBindDN “CN=bindaccount,CN=usergroup,DC=subdomain,DC=mydomain,DC=edu”
AuthLDAPBindPassword (bindaccountpassword)
AuthLDAPAuthoritative off
require valid-user
AllowOverride None
Order deny,allow
Deny from all
Allow from IP range you want to allow access from
</Directory>
- Add lines following between <VirtualHost> and </VirtualHost>
- Edit /etc/apache2/sysconfig.d/loadmodule.conf and add following
lines:LoadModule ldap_module /usr/lib/apache2/mod_ldap.so
LoadModule auth_ldap_module /usr/lib/apache2/mod_auth_ldap.so - Restart Apache with new configurationApache2ctl stop
Apache2ctl start - Test in web browser – https://server.webaddress.com/directory_you_want_to_protect_with_ldap
Cấu hình xác thực ldap
Xác thực ldap: Cần có một server đã được thiết lập xác thực ldap - vd.
https://code.google.com/p/openssl-for-windows/downloads/list
serverxacthuldap.compnay.com
1. Chạy trên Xampp
1.1. cấu hình
mở file ở thư mục cài đặt xampp
tìm tới vd: C:\xampp\php\php.ini
;extension=php_ldap.dll
bỏ comment màu đỏ đi ->extension=php_ldap.dll
1.2. Code thực thi
<html>
<head></head>
<body>
<?php
// using ldap bind
$ldaprdn = 'username@company.com'; // ldap rdn or dn
$ldappass = '123456'; // associated password
// connect to ldap server
$ldapconn = ldap_connect("ldap://serverxacthuldap.compnay.com") // Địa chỉ domain hoặc server dùng để xác thực
or die("Could not connect to LDAP server.");
if ($ldapconn) {
// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
// verify binding
if ($ldapbind) {
echo "LDAP bind successful...";
} else {
echo "LDAP bind failed...";
}
}
?>
</body>
</html>
https://code.google.com/p/openssl-for-windows/downloads/list
serverxacthuldap.compnay.com
1. Chạy trên Xampp
1.1. cấu hình
mở file ở thư mục cài đặt xampp
tìm tới vd: C:\xampp\php\php.ini
;extension=php_ldap.dll
bỏ comment màu đỏ đi ->extension=php_ldap.dll
1.2. Code thực thi
<html>
<head></head>
<body>
<?php
// using ldap bind
$ldaprdn = 'username@company.com'; // ldap rdn or dn
$ldappass = '123456'; // associated password
// connect to ldap server
$ldapconn = ldap_connect("ldap://serverxacthuldap.compnay.com") // Địa chỉ domain hoặc server dùng để xác thực
or die("Could not connect to LDAP server.");
if ($ldapconn) {
// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
// verify binding
if ($ldapbind) {
echo "LDAP bind successful...";
} else {
echo "LDAP bind failed...";
}
}
?>
</body>
</html>
Thứ Sáu, 26 tháng 9, 2014
Ghi log trong C#
public void Log(string Message, TextWriter w)
{
w.Write("\r\nLog Entry: ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
w.WriteLine(" :");
w.WriteLine(" :{0}", Message);
w.WriteLine("-------------------------------");
}
public void DumpLog(StreamReader r)
{
string line;
while ((line = r.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
using (StreamWriter w = File.AppendText(SererLogFileMapPath + "log.txt"))
{
Log(ex.Message, w);
}
using (StreamReader tr = File.OpenText(SererLogFileMapPath + "log.txt"))
{
DumpLog(tr);
}
{
w.Write("\r\nLog Entry: ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
w.WriteLine(" :");
w.WriteLine(" :{0}", Message);
w.WriteLine("-------------------------------");
}
public void DumpLog(StreamReader r)
{
string line;
while ((line = r.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
using (StreamWriter w = File.AppendText(SererLogFileMapPath + "log.txt"))
{
Log(ex.Message, w);
}
using (StreamReader tr = File.OpenText(SererLogFileMapPath + "log.txt"))
{
DumpLog(tr);
}
Đăng ký:
Bài đăng (Atom)