打印

[NET精华教程] .net教程:.net中带有口令加密的注册页面(还是发表在这里吧~-~)

.net教程:.net中带有口令加密的注册页面(还是发表在这里吧~-~)

在asp.net中提供了加密的功能。名字空间system.web.security中包含了类formsauthentication,其中有一个方法hashpasswordforstoringinconfigfile。这个方法可以将用户提供的字符变成乱码,然后存储起来。注意此方法是不能继承的。
下面的代码就是在做注册页面时将数据加密后存储到数据库的过程
imports system.web.security
imports system.data
imports system.data.sqlclient  '////////所需要的名称空间

private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
        dim passformate as string
        '///////////////encryptpassword调用函数
        passformate = encryptpassword(uid.text, "md5")   '//////////或者是encryptpassword(uid.text, "sha1")

        'textbox2.text = encryptpassword(uid.text, "md5")
        'textbox3.text = encryptpassword(uid.text, "sha1")
        '///////////这些大家自己试验吧
        'textbox4.text = formsauthentication.formscookiename
        'textbox5.text = formsauthentication.formscookiepath
        'textbox6.text = formsauthentication.getredirecturl(uid.text, true)
        'formsauthentication.setauthcookie(uid.text, true)

        dim sql as string = "insert into pwd(uid,pwd) values(@uid,@pwd)"
        dim comm as sqlcommand = new sqlcommand(sql, conn)
        conn.open()
        comm.parameters.add(new sqlparameter("@uid", sqldbtype.char, 16))
        comm.parameters("@uid").value = uid.text
        comm.parameters.add(new sqlparameter("@pwd", sqldbtype.char, 16))

        comm.parameters("@pwd").value = passformate
        comm.executenonquery()
        
    end sub

    '////////////////定义加密函数,可以随时调用。
    function encryptpassword(byval password as string, byval passwordformate as string)
        if passwordformate = "sha1" then
            encryptpassword = formsauthentication.hashpasswordforstoringinconfigfile(password, "sha1")
        elseif passwordformate = "md5" then
            encryptpassword = formsauthentication.hashpasswordforstoringinconfigfile(password, "md5")
        else
            encryptpassword = ""
        end if

    end function
至于用户的验证也是一样





TOP

返回顶部
AYBlue

Processed in 0.040491 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-8 15:52 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP