How to call an Azure function from an ASPNET Core MVC application

first_img If everything worked out right, you will see the following page when you run your ASP.NET Core MVC application. The login form is obviously totally non-functional: However, the login form is totally responsive. If you had to reduce the size of your browser window, you will see the form scale as your browser size reduces. This is what you want. If you want to explore the responsive design offered by Bootstrap, head on over to https://getbootstrap.com/ and go through the examples in the documentation: The next thing we want to do is hook this login form up to our controller and call the Azure Function we created to validate the email address we entered. Let’s look at doing that next. Hooking it all up To simplify things, we will be creating a model to pass to our controller: Create a new class in the Models folder of your application called LoginModel and click on the Add button: 2. Your project should now look as follows. You will see the model added to the Models folder: The next thing we want to do is add some code to our model to represent the fields on our login form. Add two properties called Email and Password: namespace CoreMailValidation.Models { public class LoginModel { public string Email { get; set; } public string Password { get; set; } } } Back in the Index.cshtml view, add the model declaration to the top of the page. This makes the model available for use in our view. Take care to specify the correct namespace where the model exists: @model CoreMailValidation.Models.LoginModel @{ ViewData[“Title”] = “Login Page”; } The next portion of code needs to be written in the HomeController.cs file. Currently, it should only have an action called Index(): public IActionResult Index() { return View(); } Add a new async function called ValidateEmail that will use the base URL and parameter string of the Azure Function URL we copied earlier and call it using an HTTP request. I will not go into much detail here, as I believe the code to be pretty straightforward. All we are doing is calling the Azure Function using the URL we copied earlier and reading the return data: private async Task ValidateEmail(string emailToValidate) { string azureBaseUrl = “https://core-mail- validation.azurewebsites.net/api/HttpTriggerCSharp1″; string urlQueryStringParams = $”? code=/IS4OJ3T46quiRzUJTxaGFenTeIVXyyOdtBFGasW9dUZ0snmoQfWoQ ==&email={emailToValidate}”; In this tutorial, we’ll learn how to call an Azure Function from an ASP.NET Core MVC application. This article is an extract from the book C# 7 and .NET Core Blueprints, authored by Dirk Strauss and Jas Rademeyer. This book is a step-by-step guide that will teach you essential .NET Core and C# concepts with the help of real-world projects. We will get started with creating an ASP.NET Core MVC application that will call our Azure Function to validate an email address entered into a login screen of the application: This application does no authentication at all. All it is doing is validating the email address entered. ASP.NET Core MVC authentication is a totally different topic and not the focus of this post. In Visual Studio 2017, create a new project and select ASP.NET Core Web Application from the project templates. Click on the OK button to create the project. This is shown in the following screenshot: On the next screen, ensure that .NET Core and ASP.NET Core 2.0 is selected from the drop-down options on the form. Select Web Application (Model-View-Controller) as the type of application to create. Don’t bother with any kind of authentication or enabling Docker support. Just click on the OK button to create your project: After your project is created, you will see the familiar project structure in the Solution Explorer of Visual Studio: Creating the login form For this next part, we can create a plain and simple vanilla login form. For a little bit of fun, let’s spice things up a bit. Have a look on the internet for some free login form templates: I decided to use a site called colorlib that provided 50 free HTML5 and CSS3 login forms in one of their recent blog posts. The URL to the article is: https://colorlib.com/wp/html5-and-css3-login-forms/. I decided to use Login Form 1 by Colorlib from their site. Download the template to your computer and extract the ZIP file. Inside the extracted ZIP file, you will see that we have several folders. Copy all the folders in this extracted ZIP file (leave the index.html file as we will use this in a minute): Next, go to the solution for your Visual Studio application. In the wwwroot folder, move or delete the contents and paste the folders from the extracted ZIP file into the wwwroot folder of your ASP.NET Core MVC application. Your wwwroot folder should now look as follows: 4. Back in Visual Studio, you will see the folders when you expand the wwwroot node in the CoreMailValidation project. 5. I also want to focus your attention to the Index.cshtml and _Layout.cshtml files. We will be modifying these files next: Open the Index.cshtml file and remove all the markup (except the section in the curly brackets) from this file. Paste the HTML markup from the index.html file from the ZIP file we extracted earlier. Do not copy the all the markup from the index.html file. Only copy the markup inside the tags. Your Index.cshtml file should now look as follows: @{ ViewData[“Title”] = “Login Page”; } @ViewData[“Title”] – CoreMailValidation @RenderBody()© 2018 – CoreMailValidation using (HttpClient client = new HttpClient()) { using (HttpResponseMessage res = await client.GetAsync( $”{azureBaseUrl}{urlQueryStringParams}”)) { using (HttpContent content = res.Content) { string data = await content.ReadAsStringAsync(); if (data != null) { return data; } else return “”; } } } } Create another public async action called ValidateLogin. Inside the action, check to see if the ModelState is valid before continuing. For a nice explanation of what ModelState is, have a look at the following article—https://www.exceptionnotfound.net/asp-net-mvc-demystified-modelstate/. We then do an await on the ValidateEmail function, and if the return data contains the word false, we know that the email validation failed. A failure message is then passed to the TempData property on the controller. The TempData property is a place to store data until it is read. It is exposed on the controller by ASP.NET Core MVC. The TempData property uses a cookie-based provider by default in ASP.NET Core 2.0 to store the data. To examine data inside the TempData property without deleting it, you can use the Keep and Peek methods. To read more on TempData, see the Microsoft documentation here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?tabs=aspnetcore2x. If the email validation passed, then we know that the email address is valid and we can do something else. Here, we are simply just saying that the user is logged in. In reality, we will perform some sort of authentication here and then route to the correct controller. So now you know how to call an Azure Function from an ASP.NET Core application. If you found this tutorial helpful and you’d like to learn more, go ahead and pick up the book C# 7 and .NET Core Blueprints. Read Next What is ASP.NET Core? Why ASP.NET makes building apps for mobile and web easy How to dockerize an ASP.NET Core applicationlast_img read more

Is Antitrust regulation coming to Facebook following fake news inquiry made by

first_imgThe DCMS meeting for fake news inquiry on Facebook’s platform was held yesterday at the House of Commons, UK. This was the first time that parliamentarians from such number (nine) of countries gathered in one place. Representatives were from Argentina, Canada, France, Singapore, Ireland, Belgium, Brazil, Latvia, and a select few from the DCMS committee. Richard Allan is also a member of House of Lords, as Lord Allan of Hallam in addition to being Facebook VP for policy solutions. The committee was chaired by Damian Collins MP, head of UK parliament’s digital, culture, media and sport (DCMS) select committee. About Zuckerberg not attending the meeting himself Facebook had refused to send Zuckerberg to the hearing despite repeated requests from the DCMS committee and even after being flexible about remotely attending the meeting via FaceTime. The parliamentarians were clearly displeased with Mark Zuckerberg’s empty chair at the meeting. They made remarks about how he should be accountable as a CEO for a meeting that involves his company and representatives representing millions of Facebook users from different countries. There were plenty of remarks directed at the Facebook founder being absent in the hearing. Statement from Mark Zuckerberg to the US Senate hearing earlier this year: “We didn’t take a broad enough view of our responsibility, it was my mistake and I am sorry”. Allan was asked if he thought that was a genuine statement, he said yes. Then Nathaniel Erskine-Smith from Canada made a remark “Just not sorry enough to appear himself before nine parliaments.” Canada wasn’t done, another remark from Erksine-Smith: “Sense of corporate social responsibility, particularly in light of the immense power and profit of Facebook, has been empty as the chair beside you.” In Canada, only 270 people had used the app called Your Digital Life related to Cambridge Analytica and 620,000 had their information shared with the developer. Who gave Mr. Zuckerberg the advice to ignore this committee? Charles Angus Vice-Chair, from House of Commons, Canada made a remark that Zuckerberg decided to “blow off this meeting”. Richard Allan accepted full responsibility for decisions on public appearances for Facebook. How does it looks that Zuckerberg is not here and you’re apologizing for his absence? “Not great” was his answer. Don’t you see that Facebook has lost public trust due to misinformation tactics? Allan agreed to this point. Charles Angus said Facebook has lost the trust of the international committee that it can police itself. Damian Collins said, “It should be up to the parliaments to decide what regulatory measures need to be set in place and not Facebook.“ Were you sent because you could answer our questions or to defend Facebook’s position? Allan said that he was sent to answer questions and was in the company since 2009 and had experienced events first hand. He said that he volunteered to come, Mike Schroepfer, Facebook CTO was sent to an earlier hearing, but the committee was not happy with his answers. The Cambridge Analytica incident Questions were asked about when Facebook became aware of this incident. Allan said that it was when the story was out in the press. When did Mark Zuckerberg know about the GSR Cambridge Analytica incident? After some evasion, the answer was March 2018, as the timeline, when it was covered by the press. The same question was asked 6 months ago to Mike Schroepfer and he said he didn’t know. A follow up was if Facebook was aware of and banned any other apps that breached privacy. Allan said that there were many but on probing could not name even one. He promised to send the committee a written response to that question. After the US senate hearing in April, Zuckerberg was supposed to give a list of such apps that were banned, the committee still hasn’t got any such list. Ian Lucas MP (Wrexham, Labour) said: “You knew app developers were sharing information and the only time you took actions was when you were found out.” What were Facebook’s decision on its decisions on data and privacy controls that led to the Cambridge Analytica scandal? Allan explained that there are two versions of the way developers had access to user data: Before the 2015 policy changes, access to friends data was allowed After the changes, this access was removed Non user data is sitting on Facebook servers but they do not use it to create shadow profiles. Additionally, any third party apps are expected to have their own privacy policy which can be different from Facebook’s own privacy policy. Allan said that if any such app is found that has privacy measures that may lead to privacy issues, then they take actions but could not provide an example of having done so. Will Facebook apply GDPR standards across all countries as Zuckerberg stated? They believe that the tools, system that they built are GDPR complaint. Russian activity on Facebook From the recently seized documents, questions were asked but Allan deflected them by saying they are unverified and partial information. Why didn’t Facebook disclose that it knew Russian ads were run on its platform? The case made for the question was that no one from Facebook disclosed that information about Russian activity on its platform. It wasn’t disclosed until US Senate intelligence committee made a formal request. Allan said that their current policy at the moment is to investigate and publish any such activity. From the cache of documents obtained, a point was made about an email by a Facebook engineer in 2014 about Russian IP addresses using a Pinterest API key to pull over 3 billion data points through the ordered friends API. Allan said that the details from those seized mails/documents are unverified, partial and can be misleading. Allan stuck to his guns saying: “we will come back to you”. Facebook’s privacy controls Facebook user settings were overridden by a checkbox not in plain sight and that allowed your friends’ apps to access your data. When did Facebook change the API that overrode its own central privacy page? In November 2009, Facebook central privacy page that allowed you to “control who can see your profile and personal information”. In November 2011 the US federal trade commission made a complaint against Facebook that they allowed external app developers to access personal information. Allan responded by saying that in privacy settings, there was a checkbox to disallow access to your data by applications installed by friends. What about non-Facebook user data? They use it to link connections when that person becomes a Facebook user. They do not make any money out of it. What’s your beef with Six4Three? Their app, pikini depended on friends data. When Facebook changed the API to version 2 as mentioned above, Six4Three sued Facebook as their app won’t work anymore. Discussions on can Facebook be held accountable for its actions Allan agrees that there should be a global regulation to hold the company accountable for its actions. Are you serious about regulation on a global level? There are now tens of thousands of Facebook employees working towards securing user privacy, they were too few before. Allan agrees a global level regulation should be present, the company should be accountable for its actions and sanctions should be made against any undesirable actions by the company. Maybe this will be communicated from a global organization like the United Nations (UN). How is Facebook policing fake accounts and their networks? It is an ongoing battle. Most of fake account creation is not with political intent but with commercial intent to sell followers pushing spam etc. More clicks = more money for them. Many of these accounts are taken out within minutes of creation. “We have artificial intelligence systems that try and understand what a fake account looks like and shut them down as soon as they come up”. This is for mass creation of accounts. In political cases only or two accounts are created and act as genuine users. Facebook is still removing fake accounts related to Russia. Allan says they’re trying to get better all the time. Low-quality information has decreased by 50% on Facebook, as per research by academia. Fake users that use VPN are difficult to address. For running political ads, an account needs to be a regularly used account, driving license or passport is needed and also the payment information is stored with Facebook in addition to any information that Facebook may have. Allan says, in this case, it would be unwise since the information can be used to prosecute the fake account user even if the documents used were fake. In the context of fake ads or information Allan agreed that the judicial authority of the specific country is the best entrusted with taking down sensitive information. He gave an example—if someone claims that a politician is corrupt and he is not, taking it down is correct but if he is corrupt and it is taken down then genuine information is lost. A case of non-regulation was pointed out A hate speech Facebook comment in Sri Lanka was pointed out by Edwin Tong of Singapore. The comment was in Sinhalese and Facebook did not remove it even after reports of it being hate speech. Allan said that it was a mistake and they are heavily investing in artificial intelligence with a set of hate speech keywords that can weed out such comments. They are working through the different languages on this. How will Facebook provide transparency on the use of measures taken against fake news? There is a big push around academic study in this area. They are working with academics in this area to understand the fake news problem better. But they also want to ensure that Facebook doesn’t end up sharing user data that people would find inappropriate. How is Facebook monitoring new sign-ups and posts during elections? There should not be anonymous users. The next time they log in, there is a checkpoint that says more information is required. Would Facebook consider working actively with local election authorities to remove or flag posts that would influence voter choice? They think that this is essential. It is the judiciary system that can best decide if such posts are true or false and make a call to remove them. To make everyone feel that the election was free and fair is something Facebook can’t do on their own. What is Facebook doing to prevent misuse of its algorithms to influence elections? Change in algorithms the way it searches information generally. They now better classify low-quality content. Secondly, there is a category of borderline content which is not banned by close to being banned. But it is getting rewarded by the algorithm, work is being done to reduce it instead. Third party opinion as fact checkers for marking posts as false or true. This is about tilting the scales to higher quality less sensational content from lower quality more sensational content in the algorithm. What measures for fake news in WhatsApp? There are services that provide fake WhatsApp numbers. Report it to the company and they will take them down, says Allan. They are aware of this and its use and it needs to be a part of the election protection effort. Closing discussion After the lengthy round of grilling of the fake news inquiry, Angus reiterated that they expect Facebook to be accountable for its actions. Would you be interested in asking your friend Mr Zuckerberg if we should have a discussion about anti-trust? You and Mr. Zuckerberg are the symptoms. Perhaps the best regulation is anti trust, to break Facebook up from WhatsApp and Instagram, allow competition. Allan answers that it depends on the problem to solve. Angus jolted: “The problem is Facebook” which we need to address. It’s unprecedented economic control of every form of social discourse and communication. Angus asks Facebook to have corporate accountability. Perhaps in its unwillingness to be accountable to the international body, maybe anti-trust would be something to help get credible democratic responses from a corporation. These were of the highlights of the questions and answers at the committee meeting held on 27th November 2018, the House of Commons. We recommend you watch the complete proceeding for a more comprehensive context here. In our view, Mr Allan tried answering many of the questions during the three hour session of this fake news inquiry better than Sandberg or Zuckerberg did in their hearings, but the answers were less than satisfactory where important topics were involved regarding Facebook’s data and privacy controls. It does appear that Facebook will continue to delay, deny and deflect as much as it can. Read next Privacy experts urge the Senate Commerce Committee for a strong federal privacy bill “that sets a floor, not a ceiling” Consumer protection organizations submit a new data protection framework to the Senate Commerce Committee Facebook, Twitter open up at Senate Intelligence hearing, committee does ‘homework’ this timelast_img read more