ASP. NET core2.0 Login effect implementation

Source: Internet
Author: User
Tags httpcontext

Basis

New project, the basic operation of the new login page, mainly in the implementation of the login process encountered problems, nonsense said, let us enter the subject.

Verification Code

Asp.net.core2.0 itself provides system.drawing namespaces, but does not implement objects such as Image,graphics,bitmap, and can only be manipulated by referring to third-party DLLs.

1. Open NuGet Package Manager in VS Project, search for keyword "drawing", display many available packages, choose Install "zkweb.system.drawing" package

2, implementation of random string picture display code

Public Iactionresult Imgcode () {int Width = 100;            int Height = 30; String Chkcode = String.            Empty; Color list, for captcha, noise, noise color[] color = {Color.Black, color.red, Color.Blue, Color.green, Color.oran            GE, Color.brown, color.darkblue}; Font list, for captcha string[] Font = {"Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact"            }; Verification code of the character set, removed some easily confusing characters char[] character ={' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 8 ', ' 9 ', ' A                                ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' J ', ' K ', ' L ', ' M ', ' N ',            ' P ', ' R ', ' S ', ' T ', ' W ', ' X ', ' Y '};            Random rnd = new Random (); Generate a CAPTCHA string for (int i = 0; i < 4; i++) {Chkcode + = Character[rnd. Next (character.            Length)]; }//Save the CAPTCHA cookie//httpcookie anycookie = new HTTPCOokie ("Validatecookie"); Anycookie.            Values.add ("Chkcode", Chkcode); httpcontext.current.response.cookies["Validatecookie"].            values["Chkcode"] = Chkcode;            Bitmap bmp = New Bitmap (Width, Height);            Graphics g = graphics.fromimage (BMP);            G.clear (Color.White); Draw a noise line for (int i = 0; i < 2; i++) {int x1 = rnd.                Next (Width); int y1 = rnd.                Next (Height); int x2 = rnd.                Next (Width); int y2 = rnd.                Next (Height); Color clr = color[rnd. Next (color.                Length)];            G.drawline (new Pen (CLR), x1, y1, x2, y2); }//Draw verification code string for (int i = 0; i < chkcode.length; i++) {String fnt = fon T[rnd. Next (font.                Length)];                Font ft = new Font (FNT, +, fontstyle.bold); Color clr = color[rnd. Next (color.                Length)]; g.DrawString (Chkcode[i]. TOstring (), FT, new SolidBrush (CLR), (float) i * 20 + 20,            (float) 6); }//Draw noise for (int i = 0; i < i++) {int x = rnd. Next (BMP.                Width); int y = rnd. Next (BMP.                Height); Color clr = color[rnd. Next (color.                Length)]; Bmp.            SetPixel (x, Y, CLR);            }//Writes the CAPTCHA picture to the memory stream and outputs it in "image/png" format MemoryStream ms = new MemoryStream (); try {bmp.                Save (MS, Imageformat.png);                HttpContext.Session.SetString ("Chkcode", Chkcode);                Response.Body.Dispose (); Return File (Ms.            ToArray (), @ "Image/png"); finally {//explicitly releases the resource BMP.                Dispose ();            G.dispose (); }        }

3, running WebApplication Normal, verification code is not displayed, debugging found that there is a Session of the error prompt, compile can be normal through, run the prompt "Session has not been configured for this application or Request. ", using the session service at Startup.cs

, the code is as follows:

   Public voidconfigureservices (iservicecollection services) {services. Addmvc ().            Addsessionstatetempdataprovider (); Services.        Addsession (); }        //This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app.                Usedeveloperexceptionpage (); App.            Usebrowserlink (); }            Else{app. Useexceptionhandler ("/home/error"); } app.            Usestaticfiles (); App.            Usesession (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}");        }); }

4, compile, refresh the page, display the Verification code effect:

Login Action Implementation

Login backstage, using the session to save the login user object information, the session method did not find that can save the value is the object form, this is a bit embarrassing, is not the user's information with more than one key to store, this is a bit unscientific ah, there must be a simple way to achieve.

ASP. NET core2.0 Login effect implementation

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.