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

Suhail Mumtaz Awan

Introduction I am working with authorization in application, registered users are authorized on role based for some actions/controllers i.e

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

My question is, What if a user is logged in using external login method like facebook or google, how to authorize him.

What should be or can be done to achieve this, if someone know about that then please do help. Thanks for your time.

It's a trap

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);
                }
            }

    }

Whenever the user logs in via an external service, he is automatically mapped to the role "External user". Now you can authorize it like this

[Authorize(Roles = "Externaluser")]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET MVC Authorize user with many roles

Get UserID of logged-in user in Asp.Net MVC 5

How to save user last login date if he was logged in with external login provider using ASP .Net Identity?

ASP.NET MVC FormsAuthentication check if user logged in

How to create a custom attribute that will redirect to Login if it returns false, similar to the Authorize attribute - ASP.NET MVC

How to create a Custom Authorize Attribute by comparing User Id stored in table with Current User Id in Asp.net MVC 5?

Get user's email from Twitter API for External Login Authentication ASP.NET MVC C#

ASP.NET MVC: Get user id of currently logged in user

How can keep the user logged in even after the browser has closed with Identity ASP.NET MVC framework?

How [Authorize] attribute get to know that the user is authenticate in ASP.NET MVC, is it by using authentication token?

Using Asp.Net Core Identity in MVC, Authorize attribute is rebouncing to login page after succesfull login

How to change user login credentials in identity asp.net mvc?

How to get items for logged in user ASP.NET MVC

How to get the currently logged in user name through an interface to insert in updated by column in asp.net core mvc?

User login using Email or Username gives null if user not found in ASP.NET Core MVC

How to force the user to login to see any content using ASP.NET MVC 4

Get Logged in User Role: Asp.net MVC5

How to get Email address of external login in ASP.NET MVC 5

Tow types of user logged in the same login form - ASP.NET

How to obtain the name of the current user logged into my ASP.NET MVC website?

ASP.Net MVC 5 how to use Authorize Attribute with multiple login (Multiple user table)

Asp.Net MVC authorize a custom user which extends ApplicationUser

Successfully logged in user keeps getting redirected to AccessDeniedPath using ASP.NET MVC CookieAuthentication

ASP.NET MVC Caching - Logged-in user gets cached

ASP.NET Identity Register new user to use UserName as UserName not Email, but still not displaying who is logged in

ASP.NET Core 3.1 [Authorize] attribute redirects to login even for logged in user

How to Upload File using Application Pool identity instead of Logged User Identity- ASP.NET MVC Impersonation

How to get logged user data in ASP.NET Core 5 MVC (.NET 5)?

ASP.NET MVC How to be logout when same user is logged in from different computer