[英].NET CORE API Making Facebook Login Work With Openiddict/Identity
我有一個項目(項目A),這是一個使用Openiddict的.NET CORE API項目,其端點為/ connect / token以使用Identity來處理安全性等問題來發行JWT令牌。
我有另一個項目(項目B),這是一個非常簡單的項目,帶有一些HTML,該HTML向API發出請求以獲取訪問令牌並從API獲取數據。 這個項目也很棒。
現在,我無法解決這個問題,如何在這兩個完全獨立的項目之間使用Facebook登錄? 如果一切都在一個屋頂下,我知道如何使用它,這確實很容易,但是由於一切都是分開的,所以這種情況使我完全困惑。 因此,對於處理“ ExternalLogin”,“ ExternalLoginCallBack”邏輯(使用個人帳戶從.NET Web模板)的初學者來說,該API是什么? HTML項目? 與Facebook連接時,應該使用哪個重定向uri(API / HTML項目)? 那么誰應該在其“ Startup.cs”文件中包含以下代碼?
app.UseFacebookAuthentication(new FacebookOptions
{
AppId = "xxxxxxx",
AppSecret = "xxxxxxxxx",
Scope = { "email", "user_friends" },
Fields = { "name", "email" },
SaveTokens = true,
});
最后,如果這有幫助,這是我目前如何設置Project A:
STARTUP.CS(API)
公共無效的ConfigureServices函數:(API)
// add entity framework using the config connection string
services.AddEntityFrameworkSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// add identity
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// add OpenIddict
services.AddOpenIddict<ApplicationUser, ApplicationRole, ApplicationDbContext>()
.DisableHttpsRequirement()
.EnableTokenEndpoint("/connect/token")
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.UseJsonWebTokens()
.AddEphemeralSigningKey();
services.AddCors();
公共無效配置功能:(API)
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = false,
Audience = "http://localhost:54418/",
Authority = "http://localhost:54418/"
});
授權控制器(API)
public class AuthorizationController : Controller
{
private OpenIddictUserManager<ApplicationUser> _userManager;
public AuthorizationController(OpenIddictUserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
[HttpPost("~/connect/token")]
[Produces("application/json")]
public async Task<IActionResult> Exchange()
{
var request = HttpContext.GetOpenIdConnectRequest();
if (request.IsPasswordGrantType())
{
var user = await _userManager.FindByNameAsync(request.Username);
if (user == null)
{
return BadRequest(new OpenIdConnectResponse
{
ErrorDescription = "The username or password provided is incorrect"
});
}
var identity = await _userManager.CreateIdentityAsync(user, request.GetScopes());
// Add a custom claim that will be persisted
// in both the access and the identity tokens.
if (user.Avatar != null)
{
identity.AddClaim("user_avatar", user.Avatar,
OpenIdConnectConstants.Destinations.AccessToken,
OpenIdConnectConstants.Destinations.IdentityToken);
}
if (user.InSiteUserName != null)
{
identity.AddClaim("insite_username", user.InSiteUserName,
OpenIdConnectConstants.Destinations.AccessToken,
OpenIdConnectConstants.Destinations.IdentityToken);
}
identity.AddClaim("hasLoggedIn", user.HasLoggedIn.ToString(),
OpenIdConnectConstants.Destinations.AccessToken,
OpenIdConnectConstants.Destinations.IdentityToken);
// Create a new authentication ticket holding the user identity.
var ticket = new AuthenticationTicket(
new ClaimsPrincipal(identity),
new AuthenticationProperties(),
OpenIdConnectServerDefaults.AuthenticationScheme);
ticket.SetResources(request.GetResources());
ticket.SetScopes(request.GetScopes());
return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
}
return BadRequest(new OpenIdConnectResponse
{
Error = OpenIdConnectConstants.Errors.UnsupportedGrantType,
ErrorDescription = "The specified grant type is not supported."
});
}
}
}
我不知道它是否包含Project B中的任何內容,因為它非常基本/裸露,並且依賴於API進行所有操作。
我知道這是一個棘手且復雜的問題,我敢肯定我不會盡可能流暢地提出這個問題,因此我為此事先表示歉意,就像我之前說過的那樣,我很困惑。 謝謝!
現在,我無法解決這個問題,如何在這兩個完全獨立的項目之間使用Facebook登錄? 如果一切都在一個屋頂下,我知道如何使用它,這確實很容易,但是由於一切都是分開的,所以這種情況使我完全困惑。 因此,對於處理“ ExternalLogin”,“ ExternalLoginCallBack”邏輯(使用個人帳戶從.NET Web模板)的初學者來說,該API是什么? HTML項目?
在推薦的情況下(即,使用諸如授權代碼流或隱式流之類的交互式流時 ),授權服務器項目本身負責使用您在ASP.NET Core中配置的社交提供程序來處理外部身份驗證操作。管道。
從理論上講,最終的客戶端應用程序(即JS應用程序)甚至不知道您決定在授權服務器級別使用外部身份驗證,因為它沒有直接鏈接到Facebook或Google。
在這種情況下,在Facebook選項中配置的redirect_uri
必須對應於授權服務器應用程序擁有的終結點(在您的情況下,它由Facebook身份驗證中間件提供)。
如果您不喜歡這種方法, 那么還有另一種名為“ assertion grant”的流程 ,該流程基本上顛倒了處理方式:最終的客戶端應用程序(在您的情況下為JS應用程序)直接鏈接至Facebook-因此redirect_uri
必須對應於JS應用-並使用OpenIddict的令牌端點將Facebook令牌與您自己的服務器發布的令牌“交換”,該令牌可以與您自己的API一起使用。
有關此流程的更多信息,請閱讀將Google idToken交換為本地openId令牌c# 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.