Пример #1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            using (var db = new BaroneapiContext())
            {
                if (db != null)
                {
                    var user = new UserServices();
                    if (user != null)
                    {
                        var userExist = user.Authenticate(context.UserName, context.Password);
                        if (!(userExist == null))
                        {
                            identity.AddClaim(new Claim("role", userExist.Role));
                            identity.AddClaim(new Claim("userdisplayname", context.UserName));
                            // var props = new AuthenticationProperties(new Dictionary<string, string>());
                            var props = new AuthenticationProperties(new Dictionary <string, string>
                            {
                                {
                                    "userdisplayname", context.UserName
                                },
                                {
                                    "role", userExist.Role
                                },
                                {
                                    "success", "true"
                                }
                            });

                            var ticket = new AuthenticationTicket(identity, props);
                            context.Validated(ticket);
                        }
                        else
                        {
                            context.SetError("invalid_grant", "Provided username and password is incorrect");
                            context.Rejected();
                        }
                    }
                }
                else
                {
                    context.SetError("invalid_grant", "Provided username and password is incorrect");
                    context.Rejected();
                }
                return;
            }
        }