Asp.net mvc 5 WeChat access to VB, mvcvb

Source: Internet
Author: User

Asp.net mvc 5 Access to VB, mvcvb

Access to official documents is php, and the code copied several times on the internet is c #, that is, there is no vb. Today, I entered this pitfall to implement access authentication for the vb version.

First, write the access model according to the development document. Create a Model in the Models folder

Public Class WeChatRequestModel ''' <summary> ''' encrypted signature ''' </summary> Public Property signature $ ''' <summary> ''' timest''' </summary> Public Property timestamp $ ''' <summary> ''' random number ''' </summary> Public Property nonce $ ''' <summary> ''' random string ''' </summary> Public Property echostr $ End Class

After the model is created, create a new Controller.

For authentication, nonce, Token, and timestamp are sorted, and SHA1 is compared with signature. As a String constant, Token is written based on the Token entered during application.

Const Token = "the Token you applied"

 

The remaining code is to translate the php code. Be sure not to use outdated members such as FormsAuthentication, so that you do not need to rewrite the code when migrating to asp.net core in the future:

 1     Private Function SHA1$(str$) 2         Return Encoding.UTF8.GetString(System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(str))) 3     End Function 4     Private Function CheckSignature(data As WeChatRequestModel) As Boolean 5         Return data.signature = SHA1(String.Join("", Aggregate s In {data.nonce, Token, data.timestamp} Order By s Into ToArray)).ToLower() 6     End Function 7     <HttpGet> 8     Public Sub Authenticate(data As WeChatRequestModel) 9         If CheckSignature(data) AndAlso Not String.IsNullOrEmpty(data.echostr) Then10             Response.Write(data.echostr)11             Response.End()12         End If13     End Sub

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.