Giter Site home page Giter Site logo

vladimir-novick / netcore2.mvc.basicauthentication Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 547 KB

Simple NetCore2 MVC Web Application with basic authentication

License: MIT License

C# 66.81% CSS 4.38% JavaScript 0.11% HTML 28.70%
authentication netcore2 linux web authentication-scheme mvc basic-authentication tls12 tls13 credentials

netcore2.mvc.basicauthentication's Introduction

NetCore2.MVC.BasicAuthentication

Simple NetCore2 MVC Web Application with basic authentication

Basic authentication scheme

The "Basic" HTTP authentication scheme is defined in RFC 7617, which transmits credentials as user ID/password pairs, encoded using base64.

Basic authentication is a simple authentication scheme built into the HTTP protocol. The client sends HTTP requests with the Authorization header that contains the word Basic word followed by a space and a base64-encoded string username:password .

CSharp example:

   HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sendParam.address);
   request.ContinueTimeout = 88000;
   request.ContentType = "application/json; charset=utf-8";
   request.Accept = "application/json, text/javascript, */*";
   request.Method = "POST";
   String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(UserName_Authotication + ":" + Password_Authontification));
   request.Headers.Add("Authorization", "Basic " + encoded);

   using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
   {
      writer.Write(strRequest);
   }
   WebResponse response = request.GetResponse();
   using ( Stream stream = response.GetResponseStream()){
                json = "";

                using (StreamReader reader = new StreamReader(stream))
                {
                    while (!reader.EndOfStream)
                    {
                        json += reader.ReadLine();
                    }
                }
	}

With Tls12 Security protocol

  using System;
  using System.IO;
  using System.Net;

  namespace Crawler
 {
     public class WebReader
    {

       private CredentialCache GetCredential(string url,string WebUserName,string WebPassword)
      {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        CredentialCache credentialCache = new CredentialCache();
        credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(WebUserName,
             WebPassword));
        return credentialCache;
      }

      public string SendHttpReq(string sUrl,string WebUserName,string WebPassword)
     {
        var encoding = System.Text.Encoding.UTF8;

        string lcHtml = null;

        try
        {

            // *** Set properties
            var loHttp = (HttpWebRequest)WebRequest.Create(sUrl);

            loHttp.Timeout = 5350000;

            loHttp.Credentials = GetCredential(sUrl,WebUserName,WebPassword);
            loHttp.PreAuthenticate = true;

                // *** Retrieve request info headers
                using (var loWebResponse = (HttpWebResponse)loHttp.GetResponse())
                {
                    using (var responseStream = loWebResponse.GetResponseStream())
                    {

                        using (var loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), encoding))
                        {

                            lcHtml = loResponseStream.ReadToEnd();
                            loResponseStream.Close();

                        }
                    }
                }
        } catch ( Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return lcHtml;

      }
   }
}

JavaScript Example

	var UserName="myUserName";
	var password="myPassword";

	$.ajax({
		type: 'POST',
		url: 'myLink',
		headers: {
			"Authorization": "Basic " + btoa(UserName+":"+password);
		},
		success : function(data) {
			//Success block  
		},
		error: function (xhr,ajaxOptions,throwError){
			//Error block 
		},
	 });	

Copyright (C) 2017-2018 by Vladimir Novick http://www.linkedin.com/in/vladimirnovick ,

[email protected] , http://www.sgcombo.com , https://github.com/Vladimir-Novick

License

	 Copyright (C) 2016-2018 by Vladimir Novick http://www.linkedin.com/in/vladimirnovick

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE. 

netcore2.mvc.basicauthentication's People

Contributors

vladimir-novick avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.