https登陸 及 數位憑證 檢查 設計思路及解決方案總結

來源:互聯網
上載者:User

在一些管理嚴格的部門需要使用數位憑證進行登陸,此時需要專門的數位憑證登陸代碼才行。

下面就總結一下最近做的一個項目的情況,供大家參考。在此感謝給予無私協助的成功軟體周經理和李工。

一、https 登陸的思路

先不談網站的https配置問題,另文詳細說明。 

為了減少網站代碼的變化,一般網站都採用了,只有登陸頁面使用https,而正常頁面還是使用http方式。我的解決辦法是這樣的:

在iis中配置兩個網站,(1)是網站代碼,(2)數位憑證登陸的代碼;使用者預設登陸使用數位憑證,驗證完畢後向 資料庫 中 “https”中寫入一個隨機數(隨機產生作為臨時驗證用的id)、目前時間和一個使用者唯一標識(如社會安全號碼),然後自動轉移到(1)網站的一個專門的驗證頁面login_https.aspx,此頁面從資料庫中“https”查詢隨機數是否存在,在並且時間相差不超過20秒,則是正常登陸,提取使用者的社會安全號碼,到使用者資訊庫中提取相應的登陸資訊就可以完成登陸。

代碼:

1)數字登陸部分(我用的是usbkey)

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load'-------------------     Dim CommUrl As String = "1.1.3.4" '此處為網站(1)的ip        Dim action, Url As String        Dim Pass As Boolean = False ' 驗證通過                Dim goUrl As String        Dim IsError As Boolean = False        If Not Request.ClientCertificate.IsPresent Then                        Throw New Exception("不合法使用者禁止訪問")       End If            Dim SubjectCN As String = Request.ClientCertificate("SubjectCN")            'Dim ary As String() = SubjectCN.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)Dim ary() As string = split(SubjectCN," ")            If ary.Length <> 2 Then                Throw New Exception("不合法的認證!")                            end if                Dim __UserName As String = ary(0).Trim() '使用者名稱        'Dim __IDCard As String ' = ary(1).Trim() '社會安全號碼                              Dim sqlstr As String = "select uid,姓名,警號,單位編號,職務編號,Isadmin from 使用者  WHERE (社會安全號碼 = '" & ary(1) & "')"                 Dim db As New db                db.OpenConn()                              Dim Rs As MySqlDataReader                Rs = db.ExecuteSQL(sqlstr)                If Not Rs.HasRows Then                        Rs.Close()                    Throw New Exception("未發現您的使用者資訊")                              Else                    Rs.Close()                    '產生隨機字串用於登陸驗證                    Dim ssid As String = getSSID()                    db.ExecuteSQL("insert into https (ssid,time,社會安全號碼) values('" & ssid & "','" & Now() & "','" & ary(1) & "')")                  goUrl = "login_https.aspx?ssid=" & ssid                End If                db.CloseConn()                db = Nothing     goUrl = "http://" & CommUrl & "/" & goUrl        '此處必須使用 頁面指令碼的方式,如果使用直接重新導向的話,有出現“轉移到不安全站”的提示       Response.Write("<script>location.href('" & goUrl & "')</script>")    End Sub    Private Function getSSID() As String        '產生一個隨機的字串        Dim t, i As Int32        i = 2        Dim s, pass As String        s = "abcdefghijklmnopqistuvwsyz1234567890" '~!@#$^&*_+=        pass = ""        For i = 1 To 20            Randomize() '初始化隨機數產生器。            t = Int(Rnd() * Len(s)) + 1            pass = pass + Mid(s, t, 1)        Next        Return pass    End Function

二、網站(1)處的驗證代碼login_https.aspx

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        Dim goUrl As String        Dim ssid As String = Trim(Request.QueryString("ssid"))        Dim db As New db        Dim Rs As MySqlDataReader        db.OpenConn()        Dim sqlstr As String        Dim __IDCard As String
        sqlstr = "select 社會安全號碼,time from https  WHERE (ssid = '" & ssid & "' order by time desc )"        Rs = db.ExecuteSQL(sqlstr)        If Not Rs.HasRows Then            goUrl = "error.aspx?id=9"            rs.close()        Else            Rs.Read()            __IDCard = Rs("社會安全號碼")                       Rs.Close()            '找到接著刪除            sqlstr = "delete from https where ssid='" & ssid & "';"            sqlstr += "select uid,姓名 from 使用者  WHERE (社會安全號碼 = '" & __IDCard & "')"            If Not Rs.IsClosed Then                rs.close()            End If            Rs = db.ExecuteSQL(sqlstr)            If Not Rs.HasRows Then                goUrl = "error.aspx?id=9"                rs.close()            Else                Rs.Read()                Session.Timeout = 60                Session("uid") = CStr(Rs(0))                Session("姓名") = Rs(1)                   rs.close()                '銷毀                Session.Remove("logintime")                Session.Remove("num")                goUrl = "index.aspx"            End If            Rs.Close()        End If        Response.Redirect(goUrl)        If Rs.IsClosed Then            Rs.Close()        End If        db.CloseConn()        db = Nothing    End Sub

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.