Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect MVC Action to Identity server 4 login page manually

I installed Identity Server4 which works fine. I use an application Asp.Net Core MVC as a client application. I have a login action and when the user click the button I want to redirect to the identity server login page but I dont know how to do it:

public IActionResult Login()
{
   return <Redirect to identity server4 login page??? How to do?>
}

If I add an annotation with Authorized, it does it automatically:

[Authorized]
public IActionResult Login()
  => Work fine, it redirect to identity server login page with lots of information passed by url

How to do it manually?

like image 852
Cem Avatar asked Jan 26 '23 11:01

Cem


1 Answers

You can return a ChallengeResult from the action, using the Challenge convenience method:

public IActionResult Login() =>
    Challenge(new AuthenticationProperties
    {
        RedirectUri = "/"
    });

This challenges using the default challenge scheme, which has already been set up in your example given that the [Authorize] attribute is giving the desired result. The example above also demonstrates how to set the RedirectUri.

To learn more about what challenge means, see What exactly does Challenge mean in Asp .Net Core 3?.

like image 195
Kirk Larkin Avatar answered Jan 29 '23 01:01

Kirk Larkin