Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Question: What are others approach for SPA and MVC to trigger authentication? #1552

Closed
NelsonLamprecht opened this issue Feb 23, 2018 · 6 comments

Comments

@NelsonLamprecht
Copy link

NelsonLamprecht commented Feb 23, 2018

Is it using the MapSpaFallbackRoute and hosting the SPA on that mvc cshtml page instead of the angular cli index.html?
Is it calling an authenticated mvc api and forcing a redirect in SPA?
Other solutions with the angular cli config?

What I don't want is any unauthenticated access to the application....

@NelsonLamprecht NelsonLamprecht changed the title Question: What is the best approach for SPA and MVC to trigger authentication? Question: What others approach for SPA and MVC to trigger authentication? Feb 23, 2018
@NelsonLamprecht NelsonLamprecht changed the title Question: What others approach for SPA and MVC to trigger authentication? Question: What are others approach for SPA and MVC to trigger authentication? Feb 23, 2018
@k11k2
Copy link

k11k2 commented Feb 26, 2018

@NelsonLamprecht depends on how you trying to implement authentication. from angular side then don't go over hosting spa on cshtml else you need to go over mvc but hosting spa on cshtml which is not much recommend. Currently I'm facing few troubles by hosting spa on cshtml in prod.

MapSpaFallbackRoute and hosting the SPA on that mvc cshtml page instead of the angular cli index.html?

In new aspnet angular 5 cli template it is not smooth as before template. you need to find way to work it.

Is it calling an authenticated mvc api and forcing a redirect in SPA?

you can dot it. but need to check all your concerns like storing and passing token to angular side.

@akiander
Copy link

I have this same issue... was there a recommended approach found here?

@ADefWebserver
Copy link

ADefWebserver commented Mar 21, 2018

I cover my method here:
http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/4312/An-Angular-4-DotNetCore-2-0-Example-With-Application-Shell-and-Authentication.aspx

Basically I log in a person with code like this:

    [HttpPost]
    [AllowAnonymous]
    public IActionResult Index([FromBody]DTOAuthentication Authentication)
    {
        // LoginStatus to return
        LoginStatus objLoginStatus = new LoginStatus();
        objLoginStatus.isLoggedIn = false;
        // Get values passed
        var paramUserName = Authentication.userName;
        var paramPassword = Authentication.password;
        if ((paramUserName != null) && (paramPassword != null))
        {
            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, 
            // set lockoutOnFailure: true
            var result = _signInManager.PasswordSignInAsync(paramUserName, 
                paramPassword, false, lockoutOnFailure: false).Result;
            if (result.Succeeded)
            {
                objLoginStatus.status = "Success";
                objLoginStatus.isLoggedIn = true;
                return Ok(objLoginStatus);
            }
            if (result.RequiresTwoFactor)
            {
                objLoginStatus.status = "RequiresVerification";
                return Ok(objLoginStatus);
            }
            if (result.IsLockedOut)
            {
                objLoginStatus.status = "IsLockedOut";
                return Ok(objLoginStatus);
            }
        }
        objLoginStatus.status = "Authentication Failure";
        return Ok(objLoginStatus);
    }

@brockallen
Copy link

Many people use IdentityServer as a OIDC/OAuth2 token service to protect their APIs. The SPA code can then use something like oidc-client to obtain and manage tokens.

http://identityserver.io/
https://www.npmjs.com/package/oidc-client

samples here: http://docs.identityserver.io/en/release/quickstarts/7_javascript_client.html

@SteveSandersonMS
Copy link
Member

Hope some of the approaches described here are what you need.

I'll mark this closed since it's not an active work item, but please feel free to continue the discussion!

@akiander
Copy link

@brockallen - your suggestion of using the oidc-client is perfect, thank you. That pointed me in the right direction.

Since my application is an Angular 5 application, I was able to follow this article and am now successfully authenticating against my identity provider.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants