[英].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.