Two Factor Authentication using Google Authenticator in own asp.net project? - asp.net

Hello I have created own asp.net project (Not MVC). Now I want to implement Two Factor Authentication using Google Authenticator. So when ever user get register user will get key or get QR image and setup with it's android phone. And for login they need key from google authenticator app.
I got few MVC code in asp.net. I need steps to how integrate in asp.net application (Not MVC) Please guide how can i implement this any sample will be appreciated.
Thanks

To Add Google authentication you need the following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using System.Web.Profile;
using System.Web.Security;
using Google.Authenticator;
To get the Google.Authenticator; check here https://www.nuget.org/packages/GoogleAuthenticator
now setting up the Google authentication.
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
var setupInfo = tfa.GenerateSetupCode("Name of the app", "More info ABout the App", "SuperSecretKeyGoesHere", 300 , 300//the width and height of the Qr Code);
string qrCodeImageUrl = setupInfo.QrCodeSetupImageUrl; // assigning the Qr code information + URL to string
string manualEntrySetupCode = setupInfo.ManualEntryKey; // show the Manual Entry Key for the users that don't have app or phone
Image1.ImageUrl = qrCodeImageUrl;// showing the qr code on the page "linking the string to image element"
Label1.Text = manualEntrySetupCode; // showing the manual Entry setup code for the users that can not use their phone
you can change the SuperSecretKeyGoesHere to any value that you want, but make sure it has more than 10 character otherwise the manual entry key that is generated will not work.
Now you can check the user input with text box and button click
this bit will look at the user entry and see if its ok
string user_enter=TextBox1.Text;
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
bool isCorrectPIN = tfa.ValidateTwoFactorPIN("SuperSecretKeyGoesHere", user_enter);
if (isCorrectPIN == true)
{
Label2.Text = "i am cool";
}
else
{
Label2.Text = "i am Fool";
}

Old question but I've blogged about your case exactly, you can read the post: Two Factor Authentication in ASP.NET Web API & AngularJS using Google Authenticator
Hope this answers your question.

Related

Why does Microsoft Authenticator show email domain when using email as label rather than the Issuer?

I am using TwoFactorAuth.net to show a QR code for scanning into an authenticator app. This works if I use a name in the "label" parameter. However, if I use an email address as the "label" in creating the QR code, Microsoft Authenticator uses the domain from the email as the Issuer, rather than the provided issuer.
To wit:
EX 1 - Not Email (i.e. sample code):
private readonly TwoFactorAuth tfa = new TwoFactorAuth("MyCompany",
qrcodeprovider: new QRCoder.QRCoderQRCodeProvider());
Model.GetQrCodeImageAsDataUri("Bob Ross", (string)Session["secret"])"
RESULT:
EX 2 - Email Label:
private readonly TwoFactorAuth tfa = new TwoFactorAuth("MyCompany", qrcodeprovider: new QRCoder.QRCoderQRCodeProvider());
img src="#Model.GetQrCodeImageAsDataUri("mypaint#thebobross.com", (string)Session["secret"])"
RESULT:
However, the email DOES work properly in Google Authenticator:
Any ideas?
Here's the standard Key Uri Format:
otpauth://TYPE/LABEL?PARAMETERS
I couldn't find any substantial information on how Microsoft Authenticator parses otpauth URI but after testing different ways I have come to the conclusion that if an email address alone is given as a LABEL, Microsoft Authenticator picks the subdomain of whatever TLD is present. Actually, Wiki also says different parts of a domain name are called labels so probably that's the logic there.
Anyway, I solved it by adding Issuer before the email address as LABEL (mind the ':' separator).
"MyCompany:mypaint#thebobross.com"
I've successfully tested this with Microsoft and Google Authenticator. Both show MyCompany as LABEL.

Mail Merge Feature for a CRM web-app made in asp.NET

We're working on a web based CRM for my company in ASP.net. I frequently have to send newsletters to all of my customers, and it becomes tedious to manually copy all of their addresses. What I would like is a feature to send one mail to all of my customers, taking their addresses from our contacts database, similar to a mail merge.
My developer said that he can do this for Emails, but not for physical mail. His reasoning behind this is that he can write a script that sends the mails to all customers one by one, but he can only give one single print command, which would only be able to print the current contents of the page. Therefore, he would not be able to print the individual letters for all of the customers.
Does anyone have ideas on how this would be possible? E.g. printing the page in such a way that each letter would be printed on a seperate page, or another way to automatically print all of the letters (with the mailmerged fields)?
Any help will be appreciated. If you require more details, please tell me.
A webpage is not the right solution to physically print letters. What you need to produce is a report that would generate a PDF file. This report will generate a PDF document with a different customer address on each page. Try using Microsoft Reporting Services, it is included in SQL Server. Crystal Reports is also a popular reporting solution too.
Also, you will have a hard time printing the stylized contents of your nice looking e-mail in the reporting solutions mentioned above. Consider using the report only as the cover letter of your mail piece.
One possible solution is to use 3rd party library for creation of individual letters for your customers. Docentric Toolkit is .NET tool that solves exactly your problem. We are using it for creating individual letters for customers and they all are merged in one file so that printing is done only once. Users can even create or change template documents.
Next you would have to create a template document in MS Word where you would include fixed content and placeholders for variable content which would be filled in at runtime with customer information.
After processing the data in .NET application you merge the data with the template document (see code snippet below). Your final document will be one file with letters for your customers, each on its own page. This file can then be sent to the printer with one print command.
I am attaching a code snippet of a Main method of the sample console application. The project has references to Entity Framework and Docentric’s dlls and uses entity model of Northwind database.
As you can see, it is really easy to prepare the data and merge it with template document. Solution is suitable for ASP.NET and MVC applications because you don’t need Microsoft Office installed on the server.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Docentric.Word;
namespace DisplayCustomers
{
class Program
{
static void Main(string[] args)
{
// first we read customers - in the example we select only customers
// from USA and Canada and order them by country and customer name
List<Customers> customerList = new List<Customers>();
using (var db = new NORTHWNDEntities())
{
customerList = db.Customers
.OrderBy(o => o.Country)
.ThenBy(o => o.CompanyName)
.Where(w => w.Country == "USA" || w.Country == "Canada")
.ToList();
}
// next we merge customers data with the template and generate final document;
string templateDoc = #"C:\Test\Templates\CustomerLetter1_templ.docx";
string outputDoc = #"C:\Test\FinishedLetters\CustomerLetters1.docx";
DocumentGenerator dg = new DocumentGenerator(customerList);
DocumentGenerationResult result = dg.GenerateDocument(templateDoc, outputDoc);
}
}
}

Spring Social Twitter Authenticaion in asp.net

I'm designing a website using forms asp.net
I would like this website to view tweets for a from a twitter account timeline using spring social twitter API
the problem is when I i run the web page it show this error
The Twitter REST API v1 is no longer active. Please migrate to API
v1.1. https://dev.twitter.com/docs/api/1.1/overview.
I'm sure that I have the latest version of the library as it works fine with there console application sample code but they neither provide a sample for AS.NET forms nor a proper documentation
I'm really lost and I can't figure how to use it
anyway here's my code and I would really appreciate any help
string consumerKey = //my kry;
string consumerKeySecret = //my key secret;
string accessToken = //my token;
string accessTokenSecret = //my token secret;
ITwitter twitter = new TwitterTemplate(consumerKey, consumerKeySecret, accessToken, accessTokenSecret);
IList<Tweet> tweets = twitter.TimelineOperations.GetUserTimeline();
foreach (var tweet in tweets)
lbl.Text = lbl.Text + "\n" + tweet;
The API you are using seems to be deprecated.
I am personally used Tweetinvi for lots of developments related with Twitter and lots of features are already implemented.
Also it is compatible with Twitter 1.1.
Here is how to get the timeline of the connected user:
var user = User.GetLoggedUser();
var timeline = user.GetUserTimeline();

API (Service) to work for both Windows Phone and Website

I created(in last stage) a ASP.NET website which requires database communications, So we created WCF services to connect to the database. If there is a Select statement those service returns DataTable.
Services are working fine and website also working fine
At the time of API creation i don't know that windows phone sdk does not support the DataTables.
I read some where that Windows Phone SDK doesn't support DataTable, i checked in my Visual Studio also there is no class called DataTable in System.Data namespace.
I am new to Windows Phone.
But now we want to create a WINDOWS PHONE APP which also works same as Website, so we want to use the existing API(WCF Service).
is there is any way to accomplish this task. most of the methods return type is DataTable.
we don't want to create two API's(means services). What should I do?
How to create a Service which works for both Windows Phone and Website.
we are ready to change the change the API and according to that ready to update Website also?
thanks
Just changed the API to return Stream instead of DataTable
System.IO.StringWriter reader = new System.IO.StringWriter();
dt.WriteXml(reader, XmlWriteMode.WriteSchema);//dt is datatable
return reader;
in website small minor changes, reading the stream and assign it to a datatable
StringReader xmlReader = new StringReader(stream);
DataTable dt = new DataTable();
dt.ReadXml(xmlReader);
Update:
This approach is not suggestible(this was my first web service application), I suggest to use JSON/XML responses(with returning DTOs) instead of returning DataTable..
Following links might land you in right direction.
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
http://mono.servicestack.net/ServiceStack.Hello/
Not sure why you used datatable. Do you mean dataset?
They are all XML under the covers. Try parsing them. Try something like this:
var resultNewDataSet = XElement.Parse(xmlContent);
var result = resultNewDataSet.Descendants("Table")
.Select(t => new
{
aaa = t.Descendants("aaa").First().Value,
bbb = t.Descendants("bbb").First().Value
});
foreach (var res in result)
{
System.Diagnostics.Debug.WriteLine("aaa: " + res.aaa + " / bbb: " + res.bbb);
}

Twitter Library for ASP.NET MVC that implements REST API

I been looking for a twitter lib for my ASP.NET NET MVC 3 software, but I need to implement the REST API functions that I nor found in Twitter Helper or Twitterizer. Rest API allow me to use a Find People query.
There is another one could solve my problem?
My suggestion would be to try looking into a library, such as TweetSharp that already has wrappers around a substantial amount of the Twitter API methods. If that doesn't work for you, consider writing your own wrapper around the methods you need using libraries such as HammockRest or RestSharp that have support for working with http, oauth and other such features that may assist you.
Looking better in Twitterizer I found what I need, perform Search People, and other functions. I recommend Twitterizer to use twitter in ASP.NET MVC 3, ´cause the oAuth process has a better code.
Here the Sample code to peform Search People in twitter using Twitterizer :
UserSearchOptions options = new UserSearchOptions();
options.NumberPerPage = 40;
options.Page = 1;
TwitterResponse<TwitterUserCollection> usersResponse = TwitterUser.Search(tokens,pesquisa.Conteudo,options);
if (usersResponse.Result == RequestResult.Success)
{
StringBuilder list = new StringBuilder();
foreach (var u in usersResponse.ResponseObject)
{
list2.Append(u.Name + "-" + u.Id);
}
ViewBag.Result_Twitter = list.ToString();
}

Resources