示例#1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            UserMasterRepository _repo = new UserMasterRepository();
            var user = _repo.ValidateUser(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "Provided username and password is incorrect");
                return;
            }
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Role, user.UserRoles));
            identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
            identity.AddClaim(new Claim("Email", user.UserEmailID));
            context.Validated(identity);
        }
        /*
         * public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
         * {
         *  using (UserMasterRepository _repo = new UserMasterRepository())
         *  {
         *      var user = _repo.ValidateUser(context.UserName, context.Password);
         *      if (user == null)
         *      {
         *          context.SetError("invalid_grant", "Provided username and password is incorrect");
         *          return;
         *      }
         *      var identity = new ClaimsIdentity(context.Options.AuthenticationType);
         *      identity.AddClaim(new Claim(ClaimTypes.Role, user.UserRoles));
         *      identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
         *      identity.AddClaim(new Claim("Email", user.UserEmailID));
         *      context.Validated(identity);
         *  }
         * }
         */

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            ClientMaster client        = context.OwinContext.Get <ClientMaster>("ta:client");
            var          allowedOrigin = context.OwinContext.Get <string>("ta:clientAllowedOrigin");

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

            UserMaster user = null;

            using (UserMasterRepository _repo = new UserMasterRepository())
            {
                user = _repo.ValidateUser(context.UserName, context.Password);
                if (user == null)
                {
                    context.SetError("invalid_grant", "Provided username and password is incorrect");
                    return;
                }
            }

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

            identity.AddClaim(new Claim(ClaimTypes.Role, user.UserRoles));
            identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
            identity.AddClaim(new Claim("Email", user.UserEmailID));

            var props = new AuthenticationProperties(new Dictionary <string, string>
            {
                {
                    "client_id", (context.ClientId == null) ? string.Empty : context.ClientId
                },
                {
                    "userName", context.UserName
                }
            });

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
        }
示例#3
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            using (UserMasterRepository _repo = new UserMasterRepository())
            {
                var user = _repo.ValidateUser(context.UserName, context.Password);
                if (user == null)
                {
                    context.SetError("invalid_grant", "Provided username and password is incorrect");
                    return;
                }
                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim(ClaimTypes.Role, user.UserRoles));
                identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
                identity.AddClaim(new Claim("Email", user.UserEmailID));
                //ClaimsIdentity identity = new ClaimsIdentity(context.Options.AuthenticationType);
                //identity.AddClaim(new Claim("sub", context.UserName));
                //identity.AddClaim(new Claim("role", "user"));
                //identity.AddClaim(new Claim("Email", user.UserEmailID));
                context.Validated(identity);
            }
        }