Beispiel #1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            var tutorialActionContext = new TutorialActionContext();
            var userManager           = new UserManager <User>(new UserStore <User>(tutorialActionContext));

            var user = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }

            var userRole = userManager.GetRoles(user.Id).First();

            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));
            identity.AddClaim(new Claim(ClaimTypes.Role, userRole));    // This is, theoretically, handled automatically by OWIN. Setting role claim manually shouldn't be neccessary. But it just don't work.

            var props = new AuthenticationProperties(new Dictionary <string, string>
            {
                {
                    "role", userRole
                },
            });
            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
        }