我有一个ASP.NET Core(v2.1),应该为每个经过身份验证的用户获取所有用户的信息。
我的FE是Vue.js,并且我正在使用this软件包来允许用户通过Google进行身份验证(以某种方式可以正常工作) 我已在https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-2.1&tabs=aspnetcore2x#create-the-app-in-google-api-console中逐步进行了操作 而且有些东西不起作用。
当我在Vue组件中使用“ postmessage”作为我的redirect_uri时,我可以使用googleUser对象,但redirect_uri不正确,因此我无法在后端服务器(ASP.NET Core)中交换令牌代码。 / p>
但是,如果我使用的是真正的redirect_uri,那么我已经在Google API控制台中进行了配置 我收到“ oauth状态丢失或无效”,并且Url如文档here中所述。
似乎身份验证中间件没有初始化,但是我找不到任何解决方法。
我的Startup.cs:
public Startup(IConfiguration configuration)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appSettings.json",
optional: false,
reloadOnChange: true)
.AddEnvironmentVariables();
builder.AddUserSecrets<Startup>();
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication().AddGoogle(googleOptions =>
{
googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});
services.AddCors(options =>
{
options.AddPolicy("AllowAll", p =>
{
p.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors("AllowAll");
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}
我的Vue.js组件,使用户能够登录:
<script>
import Vue from 'vue'
import { mapMutations, mapState } from 'vuex'
export default {
data(router) {
return {
section: 'Login',
loading: '',
response: ''
}
},
methods: {
...mapMutations(['syncUser']),
signIn: function () {
// This lets me get the googleUser object rather than the auth code
Vue.googleAuth().directAccess()
Vue.googleAuth().signIn(this.onSignInSuccess, this.onSignInError)
},
onSignInSuccess: function (googleUser) {
this.syncUser({ token: googleUser, provider: 'Google' })
// This line is redirecting me to this url with the auth code and other things from Google
googleUser.grantOfflineAccess({ 'redirect_uri': 'http://localhost:1906/signin-google' }).then(function (response) {
syncUser({ token: response.code, provider: 'Google' })
//this.toggleLoading()
//this.resetResponse()
}, function (error) {
console.log(error)
})
// this.syncUser({ token: authorizationCode, provider: 'Google' })
},
onSignInError: function (error) {
this.response = 'Failed to sign-in'
console.log('GOOGLE SERVER - SIGN-IN ERROR', error)
},
toggleLoading: function () {
this.loading = (this.loading === '') ? 'loading' : ''
},
resetResponse: function () {
this.response = ''
}
}
}
</script>
谢谢:)
答案 0 :(得分:0)
似乎与Google+关闭有关。
对于ASP.NET Core 2.x,请尝试https://github.com/aspnet/AspNetCore/issues/6486
中介绍的解决方法 services.AddAuthentication().AddGoogle(o => {
o.ClientId = _configuration["CLIENT_ID"];
o.ClientSecret = _configuration["CLIENT_SECRET"];
o.UserInformationEndpoint = "https://www.googleapis.com/oauth2/v2/userinfo";
o.ClaimActions.Clear();
o.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
o.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
o.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "given_name");
o.ClaimActions.MapJsonKey(ClaimTypes.Surname, "family_name");
o.ClaimActions.MapJsonKey("urn:google:profile", "link");
o.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
});