Tạo đối tượng Microsoft.Owin.Security.Facebook.AuthenticationOptions mới trong Startup.ConfigureAuth (StartupAuth.cs), chuyển nó lên FacebookAppId, FacebookAppSecret và một AuthenticationProvider mới. Bạn sẽ sử dụng một biểu thức lambda để truyền phương thức OnAuthenticated một số mã để thêm các xác nhận quyền sở hữu vào danh tính chứa các giá trị mà bạn trích xuất từ ngữ cảnh.Identity. Điều này sẽ bao gồm access_token theo mặc định. Bạn phải thêm email vào Phạm vi. Các thuộc tính người dùng khác có sẵn từ context.User (xem liên kết ở dưới cùng chẳng hạn).
StartUp.Auth.cs
// Facebook : Create New App
// https://dev.twitter.com/apps
if (ConfigurationManager.AppSettings.Get("FacebookAppId").Length > 0)
{
var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
AppId = ConfigurationManager.AppSettings.Get("FacebookAppId"),
AppSecret = ConfigurationManager.AppSettings.Get("FacebookAppSecret"),
Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook"));
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:email", context.Email, XmlSchemaString, "Facebook"));
return Task.FromResult(0);
}
}
};
facebookOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookOptions);
}
Trong AccountController, tôi trích xuất các ClaimsIdentity từ AuthenticationManager sử dụng cookie bên ngoài. Sau đó tôi thêm nó vào danh tính được tạo bằng cookie ứng dụng. Tôi đã bỏ qua bất kỳ khiếu nại nào bắt đầu bằng "... schemas.xmlsoap.org/ws/2005/05/identity/claims" vì nó dường như phá vỡ thông tin đăng nhập.
AccountController.cs
private async Task SignInAsync(CustomUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
// Extracted the part that has been changed in SignInAsync for clarity.
await SetExternalProperties(identity);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
private async Task SetExternalProperties(ClaimsIdentity identity)
{
// get external claims captured in Startup.ConfigureAuth
ClaimsIdentity ext = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
if (ext != null)
{
var ignoreClaim = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims";
// add external claims to identity
foreach (var c in ext.Claims)
{
if (!c.Type.StartsWith(ignoreClaim))
if (!identity.HasClaim(c.Type, c.Value))
identity.AddClaim(c);
}
}
}
Và cuối cùng, tôi muốn hiển thị bất cứ điều gì giá trị không phải là từ quyền địa phương. Tôi đã tạo một chế độ xem một phần _ExternalUserPropertiesListPartial xuất hiện trên /Account/Manage page. Tôi nhận được các xác nhận quyền sở hữu trước đây được lưu trữ từ AuthenticationManager.User.Claims và sau đó chuyển nó đến chế độ xem.
AccountController.cs
[ChildActionOnly]
public ActionResult ExternalUserPropertiesList()
{
var extList = GetExternalProperties();
return (ActionResult)PartialView("_ExternalUserPropertiesListPartial", extList);
}
private List<ExtPropertyViewModel> GetExternalProperties()
{
var claimlist = from claims in AuthenticationManager.User.Claims
where claims.Issuer != "LOCAL AUTHORITY"
select new ExtPropertyViewModel
{
Issuer = claims.Issuer,
Type = claims.Type,
Value = claims.Value
};
return claimlist.ToList<ExtPropertyViewModel>();
}
Và chỉ để được thấu đáo, quan điểm:
_ExternalUserPropertiesListPartial.cshtml
@model IEnumerable<MySample.Models.ExtPropertyViewModel>
@if (Model != null)
{
<legend>External User Properties</legend>
<table class="table">
<tbody>
@foreach (var claim in Model)
{
<tr>
<td>@claim.Issuer</td>
<td>@claim.Type</td>
<td>@claim.Value</td>
</tr>
}
</tbody>
</table>
}
Ví dụ làm việc và mã hoàn chỉnh là trên GitHub: https://github.com/johndpalm/IdentityUserPropertiesSample
Và mọi phản hồi, chính xác ion, hoặc cải tiến sẽ được đánh giá cao.
Cảm ơn bạn! Bí quyết là gọi 'facebookOptions.Scope.Add (" email ")', sau đó khi bạn nói dữ liệu email được tự động thêm làm xác nhận quyền sở hữu mà không phải phân tích cú pháp dữ liệu json. Tuy nhiên bây giờ tôi có một vấn đề mới: khi sử dụng mã của bạn, hãy gọi 'AuthenticationManager.GetExternalIdentity' trong hàm gọi lại sẽ trả về null thay vì một thể hiện của' ClaimsIdentity'. Bạn có ý tưởng về những gì có thể xảy ra không? (và có, tôi đang thêm id ứng dụng thích hợp và bí mật cho đối tượng facebookOptions) – Konamiman
Ok, tìm thấy giải pháp điều tra từ đây: http://forums.asp.net/p/1927914/5516103.aspx?p=True&t= 635154678323993438. Sau đây phải thêm vào khởi tạo đối tượng facebookOptions: 'SignInAsAuthenticationType =" External "'. Tôi sẽ chấp nhận câu trả lời của bạn nhưng có thể bạn muốn chỉnh sửa câu trả lời để nó bao gồm các thủ thuật này. :-) – Konamiman
Bạn có thể vui lòng chia sẻ mã mẫu để cho biết cách bạn truy xuất thông tin bổ sung (nói Ngày sinh) bên trong nhiệm vụ actionresult gọi lại không? – Santosh