ert*_*002 3 c# authorization oauth asp.net-core discord
我是 ASP.NET Core 的新手。我尝试使用不和谐作为第 3 方登录服务登录(例如使用 facebook、google 登录)。
我可以成功登录并拥有我的用户对象、声明,并且我可以输入具有授权属性的类。下面你可以看到 UserIdentity 没问题。
但假设用户想要返回登录页面。在这种情况下,我必须将他重定向到索引,但我想通过使用 Identity 检查用户是否经过身份验证,不幸的是,它是错误的并且没有声明等。据我了解,它可能与 cookie 或其他东西有关相似的。我还对类使用不同的属性(不是授权而是允许匿名)你可以在我的 Identity 对象下面看到
我正在分享我的验证码
services.AddAuthentication(options =>
{
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.Cookie.MaxAge = options.ExpireTimeSpan;
options.SlidingExpiration = true;
options.EventsType = typeof(CustomCookieAuthenticationEvents);
options.AccessDeniedPath = "/auth/DiscordAuthFailed";
})
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration.GetValue<string>("Jwt:Issuer"),
ValidAudience = Configuration.GetValue<string>("Jwt:Audience"),
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(Configuration.GetValue<string>("Jwt:EncryptionKey")))
};
})
.AddOAuth("Discord",
options =>
{
options.AuthorizationEndpoint = "https://discord.com/api/oauth2/authorize";
options.TokenEndpoint = "https://discord.com/api/oauth2/token";
options.Scope.Add("identify");
options.Scope.Add("email");
options.Scope.Add("guilds.join");
options.Scope.Add("guilds.members.read");
options.CallbackPath = "/auth/oauthCallback";
options.ClientId = Configuration.GetValue<string>("Discord:ClientId");
options.ClientSecret = Configuration.GetValue<string>("Discord:ClientSecret");
options.UserInformationEndpoint = "https://discord.com/api/users/@me";
options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
options.ClaimActions.MapJsonKey(ClaimTypes.Name, "username");
options.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
options.ClaimActions.MapJsonKey(ClaimTypes.IsPersistent, "verified");
options.AccessDeniedPath = "/auth/DiscordAuthFailed";
options.Events = new OAuthEvents()
{
OnCreatingTicket = async context =>
{
var request = new HttpRequestMessage(HttpMethod.Get,
context.Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
var response = await context.Backchannel.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
response.EnsureSuccessStatusCode();
var user=(await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync())).RootElement;
context.RunClaimActions(user);
}
};
});
Run Code Online (Sandbox Code Playgroud)
services.AddTransient();
所以我的问题是,成功登录后在任何类/方法中访问 userIdentify 对象的最佳方法是什么?
经过很长时间的分析,我发现了问题所在。
\n我刚刚将 DefaultAuthenticateScheme 更改为 CookieAuthenticationDefaults
\nservices.AddAuthentication(options =>\n{\n options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;\n options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;\n options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;\n \n})\nRun Code Online (Sandbox Code Playgroud)\n并在我的登录方法中调用了sign方法。
\nvar result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);\n\xc2\xa0\nawait HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, result.Principal);\nRun Code Online (Sandbox Code Playgroud)\n成功登录后,我可以在任何操作中获取 HttpContext.User 对象。
\n| 归档时间: |
|
| 查看次数: |
7443 次 |
| 最近记录: |