简体   繁体   English

如何设置当前用户身份信息

[英]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.

相关问题 如何在mvc 5 Identity中获取和设置自定义用户信息? - How to get and set custom user information in mvc 5 Identity? 如何在设置HttpContext.Current.User.Identity之前检查OWIN请求? - How do I check an OWIN request before HttpContext.Current.User.Identity is set? 如何获取有关 Identity 参数值集 MaxFailedAccessAttempts 的信息 - How to get information about the Identity parameter value set MaxFailedAccessAttempts 如何使用 Azure 身份验证在 Azure Function 中获取当前用户身份? - How to get current user identity in Azure Function with Azure Authentication? WCF-获取当前用户身份 - WCF - Get Current User Identity 获取当前用户(aspnetcore 身份) - Getting the current user (aspnetcore identity) 如何在 Identity 2.0 中列出当前用户所属的角色 - How can I list the Roles the current user belongs to in Identity 2.0 如何创建常规的HttpContext.Current.User.Identity.IsAuthenticated验证? - how to create a general HttpContext.Current.User.Identity.IsAuthenticated validation? 在 Identity 中获取当前用户的电子邮件 - get current user email in Identity 获取当前的进程标识和认证信息 - Get current process identity and authentication information
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM