{"id":2214,"date":"2012-09-12T01:51:00","date_gmt":"2012-09-12T01:51:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2012\/09\/12\/integrate-openauthopenid-with-your-existing-asp-net-application-using-universal-providers\/"},"modified":"2022-08-10T06:25:55","modified_gmt":"2022-08-10T13:25:55","slug":"integrate-openauthopenid-with-your-existing-asp-net-application-using-universal-providers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/integrate-openauthopenid-with-your-existing-asp-net-application-using-universal-providers\/","title":{"rendered":"Integrate OpenAuth\/OpenID with your existing ASP.NET application using Universal Providers"},"content":{"rendered":"<p>Over the past couple of weeks I have come across lots of questions\/discussions on while OAuth\/OpenId is cool as a feature in the ASP.NET templates in Visual Studio 2012, but how do I easily integrate this into my application outside of the templates. More so how do I extend the Universal Providers to integrate OAuth\/OpenId and use other functionality such as roles etc. I am going to cover these two areas in this post using WebForms but you could integrate the same with MVC applications as well<\/p>\n<p><span style=\"background-color: #00ff00\">Update<\/span><\/p>\n<p>While the post is titled to show how you can integrate this with UniversalProviders, you can totally integrate this with SqlMembership or with your custom membership providers since&nbsp;Microsoft.AspNet.Membership.OpenAuth uses Membership APIs for creating users and login<\/p>\n<p>I posted the following webforms project template which uses sqlmembership<\/p>\n<p><a href=\"https:\/\/github.com\/rustd\/AspNetSocialLoginSqlMembership\">https:\/\/github.com\/rustd\/AspNetSocialLoginSqlMembership<\/a><\/p>\n<h3>Following are the steps of integrating OpenAuth\/OpenId into your existing application<\/h3>\n<ul>\n<ul>\n<li>I started with an empty 4.5 webapplication(yes nothing in my project except a web.config)<\/li>\n<li>Use Nuget to get the following packages<\/li>\n<ul>\n<li>DotNetOpenAuth.AspNet<\/li>\n<ul>\n<li>This package is the core package for OAuth\/OpenID protocol communication<\/li>\n<\/ul>\n<li>Microsoft.AspNet.Providers.Core<\/li>\n<ul>\n<li>This package brings in Universal Providers<\/li>\n<\/ul>\n<li>Microsoft.AspNet.Providers.LocalDb<\/li>\n<ul>\n<li>This package sets the connectionstring for the Universal Providers<\/li>\n<\/ul>\n<li>Microsoft.AspNet.Membership.OpenAuth<\/li>\n<ul>\n<li>This package provides the extension to integrate OAuth\/OpenID with Membership providers<\/li>\n<\/ul>\n<\/ul>\n<li>Change web.config to use formsauthentication<\/li>\n<ul><\/ul>\n<\/ul>\n<\/ul>\n<div id=\"codeSnippetWrapper\" style=\"margin: 20px 0px 10px;padding: 4px;border: 1px solid silver;width: 97.5%;text-align: left;line-height: 12pt;overflow: auto;font-family: 'Courier New', courier, monospace;font-size: 8pt;cursor: text;direction: ltr;max-height: 200px;background-color: #f4f4f4\">\n<div id=\"codeSnippet\" style=\"padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\"><span style=\"color: #0000ff\">&lt;<\/span><span style=\"color: #800000\">authentication<\/span> <span style=\"color: #ff0000\">mode<\/span><span style=\"color: #0000ff\">=\"Forms\"<\/span><span style=\"color: #0000ff\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">     <span style=\"color: #0000ff\">&lt;<\/span><span style=\"color: #800000\">forms<\/span> <span style=\"color: #ff0000\">loginUrl<\/span><span style=\"color: #0000ff\">=\"Default.aspx\"<\/span><span style=\"color: #0000ff\">&gt;&lt;\/<\/span><span style=\"color: #800000\">forms<\/span><span style=\"color: #0000ff\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\"><span style=\"color: #0000ff\">&lt;\/<\/span><span style=\"color: #800000\">authentication<\/span><span style=\"color: #0000ff\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/div>\n<\/div>\n<ul>\n<ul>\n<li>In App_Start Register the list of OAuth\/OpenId providers you want to use. By convention any application_start registration is done in a folder called App_Start<\/li>\n<ul><\/ul>\n<\/ul>\n<\/ul>\n<div id=\"codeSnippetWrapper\" style=\"margin: 20px 0px 10px;padding: 4px;border: 1px solid silver;width: 97.5%;text-align: left;line-height: 12pt;overflow: auto;font-family: 'Courier New', courier, monospace;font-size: 8pt;cursor: text;direction: ltr;max-height: 200px;background-color: #f4f4f4\">\n<div id=\"codeSnippet\" style=\"padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\"><span style=\"color: #008000\">\/\/ See http:\/\/go.microsoft.com\/fwlink\/?LinkId=252803 for details on setting up this ASP.NET<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">            <span style=\"color: #008000\">\/\/ application to support logging in via external services.<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">&nbsp;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">            <span style=\"color: #008000\">\/\/OpenAuth.AuthenticationClients.AddTwitter(<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">            <span style=\"color: #008000\">\/\/ consumerKey: \"your Twitter consumer key\",<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">            <span style=\"color: #008000\">\/\/ consumerSecret: \"your Twitter consumer secret\");<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">&nbsp;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">            <span style=\"color: #008000\">\/\/OpenAuth.AuthenticationClients.AddFacebook(<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">            <span style=\"color: #008000\">\/\/ appId: \"your Facebook app id\",<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">            <span style=\"color: #008000\">\/\/ appSecret: \"your Facebook app secret\");<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">&nbsp;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">            <span style=\"color: #008000\">\/\/OpenAuth.AuthenticationClients.AddMicrosoft(<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">            <span style=\"color: #008000\">\/\/ clientId: \"your Microsoft account client id\",<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">            <span style=\"color: #008000\">\/\/ clientSecret: \"your Microsoft account client secret\");<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">&nbsp;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">            OpenAuth.AuthenticationClients.AddGoogle();<\/pre>\n<p><!--CRLF--><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<ul>\n<li>Create a page to display the list of providers to use for logging in(This page reads the list configured in App_Start) In my sample I created Default.aspx.<\/li>\n<ul>\n<li>Markup<\/li>\n<ul><\/ul>\n<\/ul>\n<\/ul>\n<\/ul>\n<div id=\"codeSnippetWrapper\" style=\"margin: 20px 0px 10px;padding: 4px;border: 1px solid silver;width: 97.5%;text-align: left;line-height: 12pt;overflow: auto;font-family: 'Courier New', courier, monospace;font-size: 8pt;cursor: text;direction: ltr;max-height: 200px;background-color: #f4f4f4\">\n<div id=\"codeSnippet\" style=\"padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">&lt;asp:ListView runat=<span style=\"color: #006080\">\"server\"<\/span> ID=<span style=\"color: #006080\">\"providerDetails\"<\/span> ItemType=<span style=\"color: #006080\">\"Microsoft.AspNet.Membership.OpenAuth.ProviderDetails\"<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">             SelectMethod=<span style=\"color: #006080\">\"GetProviderNames\"<\/span> ViewStateMode=<span style=\"color: #006080\">\"Disabled\"<\/span>&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">             &lt;ItemTemplate&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                 &lt;button type=<span style=\"color: #006080\">\"submit\"<\/span> name=<span style=\"color: #006080\">\"provider\"<\/span> <span style=\"color: #0000ff\">value<\/span>=<span style=\"color: #006080\">\"&lt;%#: Item.ProviderName %&gt;\"<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                     title=<span style=\"color: #006080\">\"Log in using your &lt;%#: Item.ProviderDisplayName %&gt; account.\"<\/span>&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                     &lt;%#: Item.ProviderDisplayName %&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                 &lt;\/button&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">             &lt;\/ItemTemplate&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">             &lt;EmptyDataTemplate&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                 &lt;p&gt;There are no external authentication services configured. &lt;\/p&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">             &lt;\/EmptyDataTemplate&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">         &lt;\/asp:ListView&gt;<\/pre>\n<p><!--CRLF--><\/div>\n<\/div>\n<ul>\n<ul>\n<ul>\n<ul><\/ul>\n<li>Code<\/li>\n<ul><\/ul>\n<\/ul>\n<\/ul>\n<\/ul>\n<div id=\"codeSnippetWrapper\" style=\"margin: 20px 0px 10px;padding: 4px;border: 1px solid silver;width: 97.5%;text-align: left;line-height: 12pt;overflow: auto;font-family: 'Courier New', courier, monospace;font-size: 8pt;cursor: text;direction: ltr;max-height: 200px;background-color: #f4f4f4\">\n<div id=\"codeSnippet\" style=\"padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\"><span style=\"color: #0000ff\">public<\/span> IEnumerable&lt;ProviderDetails&gt; GetProviderNames()<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">     {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">         <span style=\"color: #0000ff\">return<\/span> OpenAuth.AuthenticationClients.GetAll();<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">     }<\/pre>\n<p><!--CRLF--><\/div>\n<\/div>\n<blockquote>\n<p>At this stage the UI will look as follows<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/0358.providers_thumb_483D7AD3.jpg\"><img decoding=\"async\" width=\"572\" height=\"179\" title=\"providers\" alt=\"providers\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/0358.providers_thumb_483D7AD3.jpg\" border=\"0\" \/><\/a><\/p>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<ul>\n<ul>\n<li>Request a call to the OpenID\/OAuth provider for RequestAuthentication. This code will make an outbound call to the provider where a user can enter the login details and the provider will call back to the app&rsquo;s return url<\/li>\n<ul><\/ul>\n<\/ul>\n<\/ul>\n<div id=\"codeSnippetWrapper\" style=\"margin: 20px 0px 10px;padding: 4px;border: 1px solid silver;width: 97.5%;text-align: left;line-height: 12pt;overflow: auto;font-family: 'Courier New', courier, monospace;font-size: 8pt;cursor: text;direction: ltr;max-height: 200px;background-color: #f4f4f4\">\n<div id=\"codeSnippet\" style=\"padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\"><span style=\"color: #0000ff\">public<\/span> <span style=\"color: #0000ff\">string<\/span> ReturnUrl { get; set; }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">&nbsp;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">        <span style=\"color: #0000ff\">protected<\/span> <span style=\"color: #0000ff\">void<\/span> Page_Load(<span style=\"color: #0000ff\">object<\/span> sender, EventArgs e)<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">        {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">            <span style=\"color: #0000ff\">if<\/span> (IsPostBack)<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">            {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                var provider = Request.Form[<span style=\"color: #006080\">\"provider\"<\/span>];<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                <span style=\"color: #0000ff\">if<\/span> (provider == <span style=\"color: #0000ff\">null<\/span>)<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                    <span style=\"color: #0000ff\">return<\/span>;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">&nbsp;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                var redirectUrl = <span style=\"color: #006080\">\"~\/ExternalLoginLandingPage.aspx\"<\/span>;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                <span style=\"color: #0000ff\">if<\/span> (!String.IsNullOrEmpty(ReturnUrl))<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                    var resolvedReturnUrl = ResolveUrl(ReturnUrl);<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                    redirectUrl += <span style=\"color: #006080\">\"?ReturnUrl=\"<\/span> + HttpUtility.UrlEncode(resolvedReturnUrl);<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">&nbsp;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                OpenAuth.RequestAuthentication(provider, redirectUrl);<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">            }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">        }<\/pre>\n<p><!--CRLF--><\/div>\n<\/div>\n<blockquote>\n<p>At this stage the UI will look as follows<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/1348.googlelogin_thumb_007BF1EC.png\"><img decoding=\"async\" width=\"437\" height=\"244\" title=\"googlelogin\" alt=\"googlelogin\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/1348.googlelogin_thumb_007BF1EC.png\" border=\"0\" \/><\/a><\/p>\n<\/blockquote>\n<ul>\n<ul>\n<li>Now when the provider calls back to the app, we have to check whether the user was authenticated without any errors and if so then login the user. In my sample user I configured the returnurl to be ExternalLoginLandingPage.aspx so create a page called ExternalLoginLandingPage in the root of your app. This page serves the following functions(For brevity, I am pasting in relevant methods\/markup here. This entire sample is posted on my github repository <a href=\"https:\/\/github.com\/rustd\/SocialLoginASPNET\">https:\/\/github.com\/rustd\/SocialLoginASPNET<\/a>)<\/li>\n<ol>\n<li>Display the authenticated username from the provider and verify if the authentication from provider succeeded or not(eg. did you enter correct username\/password)<\/li>\n<ol><\/ol>\n<\/ol>\n<\/ul>\n<\/ul>\n<div id=\"codeSnippetWrapper\" style=\"margin: 20px 0px 10px;padding: 4px;border: 1px solid silver;width: 97.5%;text-align: left;line-height: 12pt;overflow: auto;font-family: 'Courier New', courier, monospace;font-size: 8pt;cursor: text;direction: ltr;max-height: 200px;background-color: #f4f4f4\">\n<div id=\"codeSnippet\" style=\"padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">ProcessProviderResult() <span style=\"color: #0000ff\">in<\/span> page_load does <span style=\"color: #0000ff\">this<\/span> processing<\/pre>\n<p><!--CRLF--><\/div>\n<\/div>\n<ul>\n<ul>\n<ol>\n<ol><\/ol>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/1732.localaccount_thumb_189F5C47.png\"><img decoding=\"async\" width=\"527\" height=\"141\" title=\"localaccount\" alt=\"localaccount\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/1732.localaccount_thumb_189F5C47.png\" border=\"0\" \/><\/a><\/li>\n<li>You can set the local username of the user if you want to and create the membership user and associate the OAuth\/OpenID and save this to the database<\/li>\n<ol><\/ol>\n<\/ol>\n<\/ul>\n<\/ul>\n<div id=\"codeSnippetWrapper\" style=\"margin: 20px 0px 10px;padding: 4px;border: 1px solid silver;width: 97.5%;text-align: left;line-height: 12pt;overflow: auto;font-family: 'Courier New', courier, monospace;font-size: 8pt;cursor: text;direction: ltr;max-height: 200px;background-color: #f4f4f4\">\n<div id=\"codeSnippet\" style=\"padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\"><span style=\"color: #008000\">\/\/Markup and refer to codebeind methods<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">&lt;ol&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">               &lt;li <span style=\"color: #0000ff\">class<\/span>=<span style=\"color: #006080\">\"email\"<\/span>&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                   &lt;asp:Label ID=<span style=\"color: #006080\">\"Label1\"<\/span> runat=<span style=\"color: #006080\">\"server\"<\/span> AssociatedControlID=<span style=\"color: #006080\">\"userName\"<\/span>&gt;User name&lt;\/asp:Label&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                   &lt;asp:TextBox runat=<span style=\"color: #006080\">\"server\"<\/span> ID=<span style=\"color: #006080\">\"userName\"<\/span> \/&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                   &lt;asp:RequiredFieldValidator ID=<span style=\"color: #006080\">\"RequiredFieldValidator1\"<\/span> runat=<span style=\"color: #006080\">\"server\"<\/span> ControlToValidate=<span style=\"color: #006080\">\"userName\"<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">                       Display=<span style=\"color: #006080\">\"Dynamic\"<\/span> ErrorMessage=<span style=\"color: #006080\">\"User name is required\"<\/span> ValidationGroup=<span style=\"color: #006080\">\"NewUser\"<\/span> \/&gt;                    <\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">                   &lt;asp:ModelErrorMessage ID=<span style=\"color: #006080\">\"ModelErrorMessage2\"<\/span> runat=<span style=\"color: #006080\">\"server\"<\/span> ModelStateKey=<span style=\"color: #006080\">\"UserName\"<\/span> CssClass=<span style=\"color: #006080\">\"field-validation-error\"<\/span> \/&gt;                    <\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">               &lt;\/li&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">           &lt;\/ol&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: white\">           &lt;asp:Button ID=<span style=\"color: #006080\">\"Button1\"<\/span> runat=<span style=\"color: #006080\">\"server\"<\/span> Text=<span style=\"color: #006080\">\"Log in\"<\/span> ValidationGroup=<span style=\"color: #006080\">\"NewUser\"<\/span> OnClick=<span style=\"color: #006080\">\"logIn_Click\"<\/span> \/&gt;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"margin: 0em;padding: 0px;width: 100%;text-align: left;color: black;line-height: 12pt;overflow: visible;font-family: 'Courier New', courier, monospace;font-size: 8pt;direction: ltr;background-color: #f4f4f4\">           &lt;asp:Button ID=<span style=\"color: #006080\">\"Button2\"<\/span> runat=<span style=\"color: #006080\">\"server\"<\/span> Text=<span style=\"color: #006080\">\"Cancel\"<\/span> CausesValidation=<span style=\"color: #006080\">\"false\"<\/span> OnClick=<span style=\"color: #006080\">\"cancel_Click\"<\/span> \/&gt;<\/pre>\n<p><!--CRLF--><\/div>\n<\/div>\n<blockquote>\n<p>At this stage the UI will look as follows<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/4150.loggedin_thumb_65CF55D2.png\"><img decoding=\"async\" width=\"466\" height=\"146\" title=\"loggedin\" alt=\"loggedin\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/4150.loggedin_thumb_65CF55D2.png\" border=\"0\" \/><\/a><\/p>\n<\/blockquote>\n<h2>Database structure<\/h2>\n<p>Once the membership user is saved to the database, the database will have the following tables<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/8272.listoftables_thumb_4BFB1FA3.png\"><img decoding=\"async\" width=\"206\" height=\"167\" title=\"listoftables\" alt=\"listoftables\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/8272.listoftables_thumb_4BFB1FA3.png\" border=\"0\" \/><\/a><\/p>\n<p>All tables would seem familiar as they are used by Universal Providers for membership, roles, profile. The 2 new tables were created by Microsoft.AspNet.Membership.OpenAuth to integrate OAuth\/OpenId information with membership system.<\/p>\n<p><strong>UsersOpenAuthAccounts<\/strong>: This holds the information on what providers can the user login by.eg if your app is configured to use Facebook, Google then the user can login via either of them and this information will be stored here<\/p>\n<p><strong>UsersOpenAuthData<\/strong>: This table integrates the OAuth\/Openid login to the membership system.<\/p>\n<p>Following image shows how OAuth\/OpenId login information is wired to membership system.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/7776.usersdata_thumb_4AB686C4.png\"><img decoding=\"async\" width=\"612\" height=\"389\" title=\"usersdata\" alt=\"usersdata\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2012\/09\/7776.usersdata_thumb_4AB686C4.png\" border=\"0\" \/><\/a><\/p>\n<p>The membershipusername is the username in the Users table.At this stage since you have the users table populated you can create roles and add\/remove these users from roles and thus achieve OAuth\/OpenId integration with Roles as well<\/p>\n<p>This entire sample is posted on my github repository(<a href=\"https:\/\/github.com\/rustd\/SocialLoginASPNET\">https:\/\/github.com\/rustd\/SocialLoginASPNET<\/a>)<\/p>\n<p>Feel free to download it and give it a try<\/p>\n<p>&nbsp;<\/p>\n<h3>What the default templates demonstrate more than this<\/h3>\n<p>To view the default templates incase you do not have VS 2012, you can browse them at the following github repro <a href=\"https:\/\/github.com\/rustd\/ASPNETTemplates\">https:\/\/github.com\/rustd\/ASPNETTemplates<\/a><\/p>\n<ul>\n<li>How to protect against XSRF attacks<\/li>\n<li>Associate a local username\/password with OAuth\/OpenID account<\/li>\n<li>Register with more than one OpenID\/OAuth provider<\/li>\n<\/ul>\n<p>I hope this would help in integration OAuth\/OpenId easily into your application when you are not starting with the templates<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the past couple of weeks I have come across lots of questions\/discussions on while OAuth\/OpenId is cool as a feature in the ASP.NET templates in Visual Studio 2012, but how do I easily integrate this into my application outside of the templates. More so how do I extend the Universal Providers to integrate OAuth\/OpenId [&hellip;]<\/p>\n","protected":false},"author":408,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[7406,7407],"class_list":["post-2214","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-oauth","tag-openid"],"acf":[],"blog_post_summary":"<p>Over the past couple of weeks I have come across lots of questions\/discussions on while OAuth\/OpenId is cool as a feature in the ASP.NET templates in Visual Studio 2012, but how do I easily integrate this into my application outside of the templates. More so how do I extend the Universal Providers to integrate OAuth\/OpenId [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/408"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=2214"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2214\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=2214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=2214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=2214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}