Эхлээд ~/Views/Web.config доторхи кодыг ийм болгож өөрчилнө. Ингэж байж ASP.NET MVC 3 Razor syntax @Facebook бичиглэлийг танина. Үгүй бол @Microsoft.Web.Helpers.Facebook гэж бичсэн ч болно.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="Microsoft.Web.Helpers"/>
</namespaces>
</pages>
</system.web.webPages.razor>
Index.csHtml код дотор нэг иймэрхүү код бичвэл шууд Login With Facebook харагдана
@model WebApplication1.Controllers.MyModel
@{
ViewBag.Title = "Home Page";
var userData = "hi, it's simple text file" + Environment.NewLine;
var dataFile = Server.MapPath("~/App_Data/data.txt");
File.WriteAllText(@dataFile, userData);
var result = "Information saved.";
Bing.SiteUrl = "http://pl-developer.blogspot.com/";
Bing.SiteTitle = "pl-developer.blogspot.com";
}
@section header0 {
<div id="title">
<h1>My MVC Application @ViewBag.Time | @Model.name</h1>
</div>
<div id="logindisplay">
@Html.Partial("_LogOnPartial")
</div>
<nav>
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</nav>
}
<h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
@{
if (Helpers.isFacebookAuthenticated()) {
<text>Welcome, @Facebook.GetFacebookUserProfile().Name</text>
}
string baseUrl = Context.Request.Url.GetLeftPart(UriPartial.Authority);
}
</p>
@section same {
<text><b>boroo, @WarningHelpers.Warning(result)</b></text>
}
<div>
<hr />
@Facebook.GetInitializationScripts()
@Facebook.LoginButtonTagOnly("", true, "long", "short", "loginRedirect('" + @baseUrl + "')", false, "email")
<hr />
</div>
Helpers.cshtml in App_Code folder
@functions {
public static bool isFacebookAuthenticated()
{
Facebook.AppId = "169933786406617";
Facebook.AppSecret = "2eb9f7461338f1e3c0d03292416c2b41";
HttpCookieCollection cookies = HttpContext.Current.Request.Cookies;
var cookieName = "fbs_" + Facebook.AppId;
if (cookies[cookieName] == null) return false;
try {
if (Facebook.IsFacebookUserAuthenticated)
{
Facebook.UserProfile profile = Facebook.GetFacebookUserProfile();
profile.Name.ToString();
return true;
}
else return false;
} catch {
cookies[cookieName].Values.Clear();
return false;
}
}
}
1 comment:
Nice post, I can't read it but the code is good my friend.
Notes: Add the function to your view page instead of in the app code to make this work. And dont forget to set the cookie when they are authenticated (logged in with facebook)
Post a Comment