دریافت اطلاعات بیشتر از Social Provider ها در VS 2013
نویسنده: آرمین ضیاء
تاریخ: ۱۳۹۲/۱۰/۱۹ ۱۷:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
List<string> scope = newList<string>() { "email", "user_about_me", "user_hometown", "friends_about_me", "friends_photos" };
var x = newFacebookAuthenticationOptions();
x.Scope.Add("email");
x.Scope.Add("friends_about_me");
x.Scope.Add("friends_photos");
x.AppId = "636919159681109";
x.AppSecret = "f3c16511fe95e854cf5885c10f83f26f";
x.Provider = newFacebookAuthenticationProvider()
{
OnAuthenticated = async context =>
{
//Get the access token from FB and store it in the database and
//use FacebookC# SDK to get more information about the user
context.Identity.AddClaim(
new System.Security.Claims.Claim("FacebookAccessToken",
context.AccessToken));
}
};
x.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
app.UseFacebookAuthentication(x); <li>
@Html.ActionLink("FacebookInfo", "FacebookInfo","Account")
</li> //
// GET: /Account/LinkLoginCallback
publicasyncTask<ActionResult> LinkLoginCallback()
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey, User.Identity.GetUserId());
if (loginInfo == null)
{
return RedirectToAction("Manage", new { Message = ManageMessageId.Error });
}
var result = await UserManager.AddLoginAsync(User.Identity.GetUserId(), loginInfo.Login);
if (result.Succeeded)
{
var currentUser = await UserManager.FindByIdAsync(User.Identity.GetUserId());
//Add the Facebook Claim
await StoreFacebookAuthToken(currentUser);
return RedirectToAction("Manage");
}
return RedirectToAction("Manage", new { Message = ManageMessageId.Error });
} [HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Manage");
}
if (ModelState.IsValid)
{
// Get the information about the user from the external login provider
var info = await AuthenticationManager.GetExternalLoginInfoAsync();
if (info == null)
{
return View("ExternalLoginFailure");
}
var user = newApplicationUser() { UserName = model.Email };
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
result = await UserManager.AddLoginAsync(user.Id, info.Login);
if (result.Succeeded)
{
await StoreFacebookAuthToken(user);
await SignInAsync(user, isPersistent: false);
return RedirectToLocal(returnUrl);
}
}
AddErrors(result);
}
ViewBag.ReturnUrl = returnUrl;
return View(model);
} //
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
publicasyncTask<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
// Sign in the user with this external login provider if the user already has a login
var user = await UserManager.FindAsync(loginInfo.Login);
if (user != null)
{
//Save the FacebookToken in the database if not already there
await StoreFacebookAuthToken(user);
await SignInAsync(user, isPersistent: false);
return RedirectToLocal(returnUrl);
}
else
{
// If the user does not have an account, then prompt the user to create an account
ViewBag.ReturnUrl = returnUrl;
ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
return View("ExternalLoginConfirmation", newExternalLoginConfirmationViewModel { Email = loginInfo.Email });
}
} privateasyncTask StoreFacebookAuthToken(ApplicationUser user)
{
var claimsIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
if (claimsIdentity != null)
{
// Retrieve the existing claims for the user and add the FacebookAccessTokenClaim
var currentClaims = await UserManager.GetClaimsAsync(user.Id);
var facebookAccessToken = claimsIdentity.FindAll("FacebookAccessToken").First();
if (currentClaims.Count() <=0 )
{
await UserManager.AddClaimAsync(user.Id, facebookAccessToken);
} public class FacebookViewModel
{
[Required]
[Display(Name = "Friend's name")]
public string Name { get; set; }
public string ImageURL { get; set; }
} //GET: Account/FacebookInfo
[Authorize]
publicasyncTask<ActionResult> FacebookInfo()
{
var claimsforUser = await UserManager.GetClaimsAsync(User.Identity.GetUserId());
var access_token = claimsforUser.FirstOrDefault(x => x.Type == "FacebookAccessToken").Value;
var fb = newFacebookClient(access_token);
dynamic myInfo = fb.Get("/me/friends");
var friendsList = newList<FacebookViewModel>();
foreach (dynamic friend in myInfo.data)
{
friendsList.Add(newFacebookViewModel()
{
Name = friend.name,
ImageURL = @"https://graph.facebook.com/" + friend.id + "/picture?type=large"
});
}
return View(friendsList);
} @model IList<WebApplication96.Models.FacebookViewModel>
@if (Model.Count > 0)
{
<h3>List of friends</h3>
<div class="row">
@foreach (var friend in Model)
{
<div class="col-md-3">
<a href="#" class="thumbnail">
<img src=@friend.ImageURL alt=@friend.Name />
</a>
</div>
}
</div>
}
این یک مثال ساده از کار کردن با تامین کنندگان اجتماعی بود. همانطور که مشاهده میکنید، براحتی میتوانید دادههای بیشتری برای کاربر جاری درخواست کنید و تجربه کاربری و امکانات بسیار بهتری را در اپلیکیشن خود فراهم کنید.
x.Scope.Add("user_friends");