[英]How To Set Current User Identity Information
I'm trying to implement login with discord. 我正在尝试实现与不和谐的登录。 I have the user's discord information, now I need to make it so that they are authorized to view certain pages.
我有用户的不和谐信息,现在我需要进行修改,以便他们有权查看某些页面。
I've tried: 我试过了:
System.Web.HttpContext.Current.User.Identity.Name = User.id;
System.Web.HttpContext.Current.User.Identity.IsAuthenticated = true;
However, this won't work because they are read only values. 但是,这将不起作用,因为它们是只读值。
Here is what the User class currently looks like: 这是User类当前的样子:
public class User
{
public string id;
public string username;
public string discriminator;
public string avatar;
public bool verified;
public string email;
public int flags;
public int premium_type;
}
And I am checking to see if the ID exists in the database. 我正在检查ID是否存在于数据库中。 Then I am trying to have this be the current logged in user for asp.net Identity.
然后,我试图使它成为asp.net Identity的当前登录用户。
How can I set this user as the current logged in Identity? 如何将此用户设置为当前登录的Identity?
You need to instantiate the UserManager and SignInManager. 您需要实例化UserManager和SignInManager。
In your controller add these dependencies. 在您的控制器中添加这些依赖项。
private readonly UserManager _userManager;
private readonly SignInManager _signInManager;
public AccountController(
UserManager userManager,
SignInManager signInManager)
{
_signInManager = signInManager;
_userManager = userManager;
}
Then use those two managers to get the information you want 然后使用这两个管理员来获取所需的信息
var appUser = _userManager.FindByIdAsync(user.Id);
To get your application user 获取您的应用程序用户
_signInManager.SignInAsync(appUser);
Then that user will be signed in, assuming they already exist in your system. 然后假定该用户已经存在于您的系统中,则该用户将登录。 If they do not you will need to create a new user through the _userManager.
如果没有,则需要通过_userManager创建一个新用户。
Here is some reference links: 以下是一些参考链接:
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.2&tabs=visual-studio https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/identity?view=aspnetcore-2.2&tabs=visual-studio
https://docs.microsoft.com/en-us/previous-versions/aspnet/dn613290(v%3Dvs.108) https://docs.microsoft.com/zh-cn/previous-versions/aspnet/dn613290(v%3Dvs.108)
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.signinmanager-1?view=aspnetcore-2.2 https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.identity.signinmanager-1?view=aspnetcore-2.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.