Esempio n. 1
0
		public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
		{
			if(!ModelState.IsValid)
				return BadRequest(ModelState);

			var verifiedAccessToken = await VerifyExternalAccessToken(model.Provider, model.ExternalAccessToken);
			if(verifiedAccessToken == null)
				return BadRequest("Invalid Provider or External Access Token");

			var existingUser = UserManager.Find(new UserLoginInfo(model.Provider, verifiedAccessToken.user_id));
			if(existingUser != null)
				return BadRequest("External user is already registered");

			var newUser = new ApplicationUser
			{
				UserName = model.UserName,
				Email = model.Email
			};

			var createResult = await UserManager.CreateAsync(newUser);
			if(!createResult.Succeeded)
				return GetErrorResult(createResult);

			var info = new ExternalLoginInfo
			{
				DefaultUserName = model.UserName,
				Login = new UserLoginInfo(model.Provider, verifiedAccessToken.user_id)
			};

			var addLoginResult = await UserManager.AddLoginAsync(newUser.Id, info.Login);
			if(!addLoginResult.Succeeded)
				return GetErrorResult(addLoginResult);

			var identity = await newUser.GenerateUserIdentityAsync(UserManager, OAuthDefaults.AuthenticationType);
			var accessTokenResponse = GenerateLocalAccessTokenResponse(identity);
			return Ok(accessTokenResponse);
		}
Esempio n. 2
0
		public async Task<IHttpActionResult> Register(RegisterBindingModel model)
		{
			if(!ModelState.IsValid)
				return BadRequest(ModelState);

			var user = new ApplicationUser
				{
					UserName = model.Email,
					Email = model.Email
				};

			var result = await UserManager.CreateAsync(user, model.Password);
			if(!result.Succeeded)
				return GetErrorResult(result);

			return Ok();
		}