In My last
post, I explained how we can implement Facebook login with OpenId selector tool. In this post I will explain you how we can use that tool in an ASP.NET MVC3 application.
1- Open Visual Studio 2010 go to File > New > Project > Web > ASP.NET MVC 3 Application:
then Choose Internet Application be sure to have Razor as your View engine and Click OK:
2- Download
DotNetOpenAuth dll and
Updated OpenID Selector files that we will use.
A- Add the DotNetOpenAuth.dll to references in your site.
B- Copy all contents of openid-selector\css folder to the 'Content' folder .
C- Copy images, images.large, images.small folders to the site 'Content' folder.
D- Copy openid-jquery.js, openid-en.js files from openid-selector\js folder to 'Scripts' folder
Your Solution Explorer will look like this (See Highlighted Portion) :
3- Go to Views > Shared > _Layout.cshtml and replace the <head> with this new head with new styles and scripts:
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/openid-shadow.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/openid.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/openid-jquery.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/openid-en.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript" src="https://connect.facebook.net/en_US/all.js"> </script>
<script type="text/javascript">
$(document).ready(function () {
openid.init( });
</script>
</head>
<span class="kwrd"><span style="color: black;">4- Go to Models > AccountModels.cs , navigate to <span style="color: blue;">public class</span> LogOnModel</span></span>
<span class="kwrd"> <span style="color: black;">and Add <span style="color: blue;">OpenID</span> attribute that we will use it to hold the returned OpenID from OpenID-Selector</span></span>
<span class="kwrd"><span style="color: black;"> your class will look like this:</span></span>
public class LogOnModel
{
[Display(Name = "OpenID")]
public string OpenID { get; set; }
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
<span class="kwrd"><span style="color: black;">5- </span></span><span class="kwrd"><span style="color: black;">Go to Models > AccountModels.cs and Add following class to it:</span></span>
public class UserDetailsModel
{
public string OpenID { get; set; }
public string ProviderUrl { get; set; }
public string FriendlyIdentifier { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Dob { get; set; }
public string Gender { get; set; }
public string Email { get; set; }
}
<span class="kwrd"><span style="color: black;"> </span></span><span class="kwrd"><span style="color: black;"> </span></span>
<span class="kwrd"><span style="color: black;">6- go to Views > Account > LogOn.cshtml </span></span><span class="kwrd"><span style="color: black;">replace all the markup with this one, This will</span></span>
<span class="kwrd"><span style="color: black;"> integrate updated OpenID-Selector to LogOn View:</span></span><span class="kwrd"><span style="color: black;"> </span></span>
<span class="kwrd"><span style="color: black;"> </span></span>
<span class="kwrd"><span style="color: black;"> </span></span>
@model FBOpenIDMVC3.Models.LogOnModel
@{
ViewBag.Title = "Log On";
}
<h2>
Log On</h2>
<p>
Please enter your username and password. @Html.ActionLink("Register", "Register")
if you don't have an account.
</p>
<script type="text/javascript">
$(document).ready(function () {
window.fbAsyncInit = function () {
FB.init({
appId: '188220747944294', channelUrl: '//' + window.location.hostname + '/channel', scope: 'id,name,first_name,last_name,gender,email',
status: true, cookie: true, xfbml: true });
};
});
function FBLogin() {
FB.login(FBCallBack);
}
function FBCallBack(response) {
if (response.authResponse) {
FB.api('/me?fields=id,name,first_name,last_name,gender,email,birthday', function (userDetail) {
if (userDetail.name) {
var url = '@Url.Action("ShowUserDetails", "Account", new { OpenID = "_id_", FriendlyIdentifier = "_id_", FirstName = "_first_", LastName = "_last_", Dob = "_birthday_", Gender = "_gender_", Email = "_email_" })';
url = url.replace('_id_', userDetail.id);
url = url.replace('_id_', userDetail.id);
url = url.replace('_first_', userDetail.first_name);
url = url.replace('_last_', userDetail.last_name);
url = url.replace('_birthday_', userDetail.birthday);
url = url.replace('_gender_', userDetail.gender);
url = url.replace('_email_', userDetail.email);
window.location.href = url;
}
});
}
}
</script>
<form action="Authenticate?ReturnUrl=@HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"])" method="post" id="openid_form">
<input type="hidden" name="action" value="verify" />
<div>
<fieldset>
<legend>Login using OpenID</legend>
<div class="openid_choice">
<p>
Please click your account provider:</p>
<div id="openid_btns">
</div>
</div>
<div id="openid_input_area">
@Html.TextBox("openid_identifier")
<input type="submit" value="Log On" />
</div>
<noscript>
<p>
OpenID is service that allows you to log-on to many different websites using a single
indentity. Find out <a href="http://openid.net/what/">more about OpenID</a> and
<a href="http://openid.net/get/">how to get an OpenID enabled account</a>.</p>
</noscript>
<div>
@if (Model != null)
{
if (String.IsNullOrEmpty(Model.UserName))
{
<div class="editor-label">
@Html.LabelFor(model => model.OpenID)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.OpenID)
</div>
<p class="button">
@Html.ActionLink("New User ,Register", "Register", new { OpenID = Model.OpenID })
</p>
}
else
{
<p class="buttonGreen">
<a href="@Url.Action("Index", "Home")">Welcome , @Model.UserName, Continue..." </a>
</p>
}
}
</div>
</fieldset>
</div>
</form>
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm())
{
<div>
<fieldset>
<legend>Or Login Normally</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
<p >
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
7- Update the value of
img_path in
openid-jquery.js to
'../Content/images/'8- Now let us run the project , then click the [Log On] link , you will get like this page:
<span class="kwrd"><span style="color: black;">9- Now to implement Open ID using DotnetOpenAuth, Go to Controllers > </span></span>
<span class="kwrd"><span style="color: black;">AccountController.cs and Add these using:</span></span>
<span class="kwrd"><span style="color: black;"> </span></span>
10 - Right Click on Views--> Accounts and Click Add-->View to add new view like this: