Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

identityserver4 - IdentityServer 4 Restfull Login/Logout

Been looking into the identity server 4 solution to compliment my ASP CORE api. Using a SPA page on front end, does IdentityServer4 have the capability to manage restfull calls for login/logout/other?

Currently my solution works perfectly to redirect to and from the IdentityServer4 solution, but wondering if i can improve on UX by avoiding the redirects that occur on login/logout?

I've heard of PopUp and iFrame capability, but from research that opens up other risks.

(not sure if this question is for stackoverflow or software engineering stack, happy to move it)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may do this by using the resource owner password grant type, where you could provide your own login screen and pass the information to IdentityServer.

In IdentityServer you would implement the IResourceOwnerPasswordValidator interface to validate the users.

In your Startup.ConfigureServices add the following.

Services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();

Here is a sample ResourceOwnerPasswordValidator class.

public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
    {
        private IUserManager _myUserManager { get; set; }

        public ResourceOwnerPasswordValidator(IUserManager userManager)
        {
            _myUserManager = userManager;
        }        

        public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {            
            var user = await _myUserManager.Find(context.UserName, context.Password);

            if (user != null)
            {
                context.Result = new GrantValidationResult(
                        subject: user.USER_ID,
                        authenticationMethod: "custom",
                        claims: await _myUserManager.GetClaimsAsync(user));
            }
            else
            {                 
                context.Result = new GrantValidationResult(
                        TokenRequestErrors.InvalidRequest, 
                        errorDescription: "UserName or Password Incorrect.");
            }             
        }
    }

The IUserManager implements the logic to check the database to validate the user.

Then the SPA client would use the GrantTypes.ResourceOwnerPassword. Here is an example you could start with.

DISCLAIMER This is not the recommended flow to use.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...