我试图让OAuth第一次为我工作。使用this作为基础,我创建了一个开箱即用的MVC应用程序,其身份验证设置为"个人用户帐户"。我已将我的Startup.Auth.cs文件修改为这样......
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
var googleOptions = new GoogleOAuth2AuthenticationOptions()
{
ClientId = "blah",
ClientSecret = "blah-blah",
CallbackPath = new PathString("/Account/ExternalLoginCallback"),
Provider = new GoogleOAuth2AuthenticationProvider
{
OnAuthenticated = async ctx =>
{
string accessToken = ctx.AccessToken;
string googleName = ctx.Name;
string googleEmailAddress = ctx.Email;
var serializedUser = ctx.User;
}
}
};
googleOptions.Scope.Add("https://www.googleapis.com/auth/drive.file");
app.UseGoogleAuthentication(googleOptions);
}
}
显然,在我的真实代码中,我有ClientId和ClientSecret的真实值。
当我运行应用程序时,会显示起始页面。
当我点击&#34;登录&#34;我看到了通过Google登录的选项。
点击它会显示“同意”屏幕。
但这是我不明白的。当我点击允许时,我的应用会重定向到http://localhost/WebApplication2/Account/ExternalLoginCallback?error=access_denied#