c# - 使用依赖注入时在 ASP.NET MVC 中注册 FacebookAuthentication
问题描述
目前在Startup.cs
我IAppBuilder
根据web.config
信息在 Facebook 提供商中注册。
现在我想移动逻辑,以便根据数据库中的数据加载 Facebook应用程序 ID和应用程序机密,或者如果不存在则从web.config
.
最初:
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
AppId = ConfigurationManager.AppSettings["fb_appid"],
AppSecret = ConfigurationManager.AppSettings["fb_appsecret"]
});
现在,我有从数据库中检索这些 id 和 secret 的存储库。
我用默认的 fb 身份验证更改了该行:
app.CreatePerOwinContext(() => new AppBuilderProvider(app));
和
public class AppBuilderProvider : IDisposable
{
private IAppBuilder _app;
public AppBuilderProvider(IAppBuilder app)
{
_app = app;
}
public IAppBuilder Get() { return _app; }
public void Dispose() { }
public void RegisterFacebook(string appId, string appSecret)
{
_app.UseFacebookAuthentication(new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions
{
AppId = appId,
AppSecret = appSecret
});
}
}
在控制器中我使用:
[ChildActionOnly]
public ActionResult LoadExternalLogin()
{
var appBuilder = HttpContext.GetOwinContext().Get<AppBuilderProvider>();
if (_context.IsSocialNetworkLoginEnabled)
{
appBuilder.RegisterFacebook(_context.FacebookAppId, _context.FacebookAppSecret);
}
return PartialView("_ExternalLogin");
}
在部分视图中:
@{
var loginProviders = Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes();
if (loginProviders.Any())
{
<div class="col-xs-5 separator"></div>
<div class="col-xs-2"><span class="text-center" data-i18n="Login_SocialNetworkLoginLabel"></span></div>
<div class="col-xs-5 separator"></div>
<div class="col-xs-12"> </div>
using (Html.BeginFormEx(Model.Action, "Account", new { ReturnUrl = Model.ReturnUrl }, FormMethod.Post, null))
{
foreach (AuthenticationDescription p in loginProviders)
{
<button type="submit" class="btn btn-primary btn-social" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" ><span class="socialNetworkLoginIcon icon_@p.AuthenticationType.ToLowerInvariant()"></span><span class="socialNetworkLoginText" data-i18n="Button_LoginWith_@p.AuthenticationType.ToLowerInvariant()">@p.AuthenticationType</span><span class="glyphicon glyphicon-menu-right"></span></button>
}
}
}
}
问题是它loginProviders
是空的,似乎无法使用我的方法注册 Facebook 身份验证。
如果我使用 withstartup.cs
方法,则loginProviders
集合中有一项。
为什么 ?
解决方案
推荐阅读
- c# - 实体框架 Reload() 错误 无效操作
- firebase - Firebase Messaging getToken() throws: Cannot read property 'buffer' of undefined
- c# - User is authenticated but where is the access token?
- javascript - jQuery将数组的倒数第二个元素移动到第一个。如何实现?
- material-ui - JSS 审讯
- spring-rabbit - 即使保存失败,WebFlux/Reactive Spring RabbitmMq 消息也会得到确认
- mysql - sql,限制时间
- linux - 我的 Makefile 中的 Bash 命令 ID 不起作用
- c# - ClassName.PropertyName 的正则表达式
- android - 如何修改位图的高度和宽度