ASP.NET Owin OAuth(Google/Facebook)重定向到远程登录页面的默认login.aspx insead [英] ASP.NET Owin OAuth (Google / Facebook) is redirecting to default login.aspx insead of remote log in page
问题描述
我正在使用Owin库(包括Google和Facebook)设置OAuth.
I'm setting up OAuth using the Owin libraries including Google and Facebook.
从外观上看,Owin启动类正在注册.我发现的是,我没有被重定向到Facebook或Google的相应登录页面,而是被重定向到默认的"login.aspx"页面.我的解决方案中没有login.aspx页面.
The Owin startup class is registering fine by the looks of it. What I'm finding is that rather than being redirected to the appropriate sign in page at Facebook or Google, I'm being redirected to a default 'login.aspx' page. There is no login.aspx page in my solution.
在这样的视图中触发流:
The flow is triggered in a view like so:
@{
// Get list of configured external authentication middleware
var loginProviders = Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes();
if (!loginProviders.Any())
{
<div>
<p>There are no external authentication services configured</p>
</div>
}
else
{
using (Html.BeginForm("ExternalLogin", "OAuth"))
{
@Html.AntiForgeryToken()
<div>
<p>
@foreach (AuthenticationDescription p in loginProviders)
{
<button type="submit" class="btn btn-default" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>
}
</p>
</div>
}
}
}
这会触发质询结果,但是质询结果只是导致重定向到login.aspx(再次不存在)
This triggers the challenge result, however the challenge result simply causes a redirect to login.aspx (which again does not exist)
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider)
{
string redirectUri = Url.Action("ExternalLoginCallback");
// Request a redirect to the external login provider
return new ChallengeResult(provider, redirectUri);
}
我可能会缺少什么?
为了方便起见,我包含了Startup.cs类:
I've included the Startup.cs class for good measure:
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(
new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
AppId = Config.OAuthFacebookAppId,
AppSecret = Config.OAuthFacebookAppSecret,
Scope = { "email" }, // "email", also "publish_actions" can be included if post to facebook authorization is required
Provider = new FacebookAuthenticationProvider
{
OnAuthenticated = context =>
{
context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
return Task.FromResult(true);
}
}
});
app.UseGoogleAuthentication(
clientId: Config.OAuthGoogleClientId,
clientSecret: Config.OAuthGoogleClientSecret
);
}
推荐答案
关键修改是添加代码:
// Stop execution of the current page/method - the 401 forces OWIN to kick-in and do its thing
Response.StatusCode = 401;
Response.End();
AuthenticationProperties.RedirectUri不会在Challenge中传递给Google( )
其他问题是未启用Google+ API
Other issues were that the Google+ API was not enabled
OWIN的GetExternalLoginInfoAsync始终返回null
...并且对于Facebook,需要将Owin库升级到3.1.0
... and for Facebook, an upgrade of the Owin libs to 3.1.0 was required
如此完整的ExternalLogin方法:
So full ExternalLogin method:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public void ExternalLogin(string provider)
{
string redirectUri = Url.Action("ExternalLoginCallback");
var properties = new AuthenticationProperties() { RedirectUri = redirectUri };
HttpContext.GetOwinContext().Authentication.Challenge(properties, provider);
// Stop execution of the current page/method - the 401 forces OWIN to kick-in and do its thing
Response.StatusCode = 401;
Response.End();
}
这篇关于ASP.NET Owin OAuth(Google/Facebook)重定向到远程登录页面的默认login.aspx insead的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!