Example #1
0
        // Called when a request to the Token endpoint arrives with a "grant_type" of "password".
        // This occurs when the user has provided name and password credentials directly
        // into the client application's user interface, and the client application is using
        // those to acquire an "access_token" and optional "refresh_token".
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            var userManager = context.OwinContext.GetUserManager <GbmonoUserManager>();

            // lookup user by user name and password
            GbmonoUser user = await userManager.FindAsync(context.UserName, context.Password);

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

            // create user identity for Bearer token
            ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType);

            // create user identity for cookie
            ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, CookieAuthenticationDefaults.AuthenticationType);

            // create properties, user name or other extra information
            AuthenticationProperties properties = CreateProperties(user);

            // initialize a new instance of the Microsoft.Owin.Security.AuthenticationTicket
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);

            // call the context.Validated(ticket) to tell the OAuth server to protect the ticket as an access token and send it out in JSON payload.
            // to issue an access token the context.Validated must be called with a new ticket containing the claims about the resource owner
            // which should be associated with the access token.
            context.Validated(ticket);

            // Signs the cookie identity so it can send the authentication cookie.
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }
Example #2
0
 public async Task<IHttpActionResult> Update(GbmonoUser user)
 {
     var userInfo = await UserManager.FindByNameAsync(RequestContext.Principal.Identity.Name);
     userInfo.DisplayName = user.DisplayName;
     userInfo.PhoneNumber = user.PhoneNumber;
     UserManager.Update(userInfo);
     return Ok(userInfo);
 }
Example #3
0
        public async Task <IHttpActionResult> Update(GbmonoUser user)
        {
            var userInfo = await UserManager.FindByNameAsync(RequestContext.Principal.Identity.Name);

            userInfo.DisplayName = user.DisplayName;
            userInfo.PhoneNumber = user.PhoneNumber;
            UserManager.Update(userInfo);
            return(Ok(userInfo));
        }
Example #4
0
        /// <summary>
        /// return user name, display name and other user related info
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static AuthenticationProperties CreateProperties(GbmonoUser user)
        {
            // extract the user profile name from user name (email)
            var userDisplayName = user.DisplayName;

            // if profile display name exists in db

            IDictionary <string, string> data = new Dictionary <string, string>
            {
                { "userName", user.UserName },
                { "displayName", userDisplayName }
            };

            return(new AuthenticationProperties(data));
        }
Example #5
0
        public async Task <IHttpActionResult> Create([FromBody] UserBindingModel model)
        {
            var displayName = model.UserName.Split('@')[0];
            var user        = new GbmonoUser()
            {
                UserName = model.UserName, Email = model.Email, CreateTime = DateTime.Now, DisplayName = displayName
            };
            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            return(Ok());
        }
Example #6
0
        public async Task<IHttpActionResult> Create([FromBody]UserBindingModel model)
        {
            // check if user name already exists
            var existedUser = UserManager.FindByName(model.UserName);

            if(existedUser != null)
            {
                return new DataInvalidResult(string.Format("{0} 已经存在.", model.UserName), Request);
            }

            var displayName = model.UserName.Split('@')[0];
            // we use email as username in gbmoni user db 
            var user = new GbmonoUser() { UserName = model.UserName, Email = model.Email, CreateTime = DateTime.Now, DisplayName = displayName };
            IdentityResult result = await UserManager.CreateAsync(user, model.Password);
            if (!result.Succeeded)
            {
                return new DataInvalidResult("注册失败.", Request);
            }
            return Ok();
        }
        /// <summary>
        /// return user name, display name and other user related info 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static AuthenticationProperties CreateProperties(GbmonoUser user)
        {
            // extract the user profile name from user name (email)
            var userDisplayName = user.DisplayName;

            // if profile display name exists in db
    
            IDictionary<string, string> data = new Dictionary<string, string>
            {
                { "userName", user.UserName },
                { "displayName", userDisplayName }
            };
            return new AuthenticationProperties(data);
        }