MVC external authentication

Mark Elliott asked on May 21, 2021 23:08

I'm working on moving one of our sites to MVC (Kentico 12). One area of the site is secured for members only so I'm implementing authentication against the external application that stores our user data.

I've been referring to https://docs.xperience.io/k12sp/managing-users/user-registration-and-authentication/external-authentication-on-mvc-sites as a reference but there is one part where I have hit a stumbling block.

// Requests a redirect to the external sign-in provider
    // Sets a return URL targeting an action that handles the response
    // Fill in the name of your controller
    return new ChallengeResult(provider, Url.Action("SignInCallback", "ExternalAuthentication", new { ReturnUrl = returnUrl }));

Since I am not using Google or Facebook or anything like that to authenticate I'm unclear what the value of provider should be here. Any ideas?

Thanks!

Correct Answer

Dmitry Bastron answered on May 24, 2021 14:41

Hi Mark,

What external application are you connecting then? Does it support Open ID Connect? On the link you mentioned all providers are registered in the very first piece of code here with .UseOpenIdConnectAuthentication method for example. And later in this article, in View code Example of external authentication buttons these providers are displayed to the user. And @p.AuthenticationType is what gets sent to your challenge method in the end when someone clicks this button from this view.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Mark Elliott answered on May 25, 2021 00:06

Hi Dmitry,

Thanks, that is good to know what provider to use. We are authenticating against iMIS which uses OAuth2

Finally managed to get user to authenticate by doing this:

var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.NameIdentifier, userId));
claims.Add(new Claim(ClaimTypes.Name, userFullName));
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

AuthenticationManager.SignIn(new AuthenticationProperties() { AllowRefresh = true, 
            IsPersistent = true }, identity);
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.