How to authorize a user who is logged in using an external login to Asp.Net MVC

Introduction I work with authorization in the application, registered users are authorized in a role based on some actions / controllers i.e.

[Authorize(Roles = "Developer,Admin,User")]

My question is: what should I do if a user logs in using an external login method, such as facebook or google, how to allow it.

What needs to be or can be done to achieve this, if someone knows about this, then please help. Thank you for your time.

+4
source share
2 answers

Do it like this:

public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
    {
          var user = new ApplicationUser { UserName = model.Email, Email = model.Email, DisplayName=model.Displayname };
            var result = await UserManager.CreateAsync(user);
            if (result.Succeeded)
            {
                result = await UserManager.AddLoginAsync(user.Id, info.Login);
                if (result.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "ExternalUser");// This is the important line

                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    return RedirectToLocal(returnUrl);
                }
            }

    }

, , " ".

[Authorize(Roles = "Externaluser")]
+4

"" , , [Authorize(Roles = "Developer,Admin,User"] .

, , Visual Studio, - ASP.NET MVC . , (Facebook, Twitter ..).

, / .

+2

Source: https://habr.com/ru/post/1651664/


All Articles