<commit_before>﻿using AutoMapper;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using ResourceMetadata.Model;
using ResourceMetadata.Service;
using ResourceMetadata.API.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace ResourceMetadata.API.Controllers
{
    public class AccountController : ApiController
    {

        private readonly IUserService userService;

        private readonly UserManager<ApplicationUser> userManager;
        private IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.Current.GetOwinContext().Authentication;
            }
        }

        public AccountController(IUserService userService, UserManager<ApplicationUser> userManager)
        {
            this.userService = userService;
            this.userManager = userManager;

            //Todo: This needs to be moved from here.
            this.userManager.UserValidator = new UserValidator<ApplicationUser>(userManager)
            {
                AllowOnlyAlphanumericUserNames = false
            };
        } 

        [HttpPost]
        [OverrideAuthorization]
        public async Task<IHttpActionResult> Post(RegisterViewModel viewModel)
        {
            if (ModelState.IsValid)
            {                
                try
                {
                    ApplicationUser user = new ApplicationUser();
                    Mapper.Map(viewModel, user);
                    
                    var identityResult = userManager.Create(user, viewModel.Password);
                    //var userRoleResult = userManager.AddToRole(user.Id, "Member");

                    if (identityResult.Succeeded)
                    {
                        var userRoleResult = await userManager.AddToRoleAsync(user.Id, "Member");

                        if (userRoleResult.Succeeded)
                        {
                            //await SignInAsync(user, isPersistent: false);
                            //return RedirectToAction("Index", "Home");
                            //return Json(new { status = "success" });
                            return Ok();
                        }

                        return InternalServerError();
                    }
                    else
                    {
                        foreach (var error in identityResult.Errors)
                        {
                            //ModelState.AddModelError(error);
                        }

                        return InternalServerError();
                    }
                }
                catch (Exception ex)
                {

                    throw ex;
                }<commit_msg>Removed code to add user role on Registration<commit_after>﻿using AutoMapper;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using ResourceMetadata.Model;
using ResourceMetadata.Service;
using ResourceMetadata.API.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace ResourceMetadata.API.Controllers
{
    public class AccountController : ApiController
    {

        private readonly IUserService userService;

        private readonly UserManager<ApplicationUser> userManager;
        private IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.Current.GetOwinContext().Authentication;
            }
        }

        public AccountController(IUserService userService, UserManager<ApplicationUser> userManager)
        {
            this.userService = userService;
            this.userManager = userManager;

            //Todo: This needs to be moved from here.
            this.userManager.UserValidator = new UserValidator<ApplicationUser>(userManager)
            {
                AllowOnlyAlphanumericUserNames = false
            };
        }

        [HttpPost]
        [OverrideAuthorization]
        public async Task<IHttpActionResult> Post(RegisterViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    ApplicationUser user = new ApplicationUser();
                    Mapper.Map(viewModel, user);

                    var identityResult = userManager.Create(user, viewModel.Password);
                    //var userRoleResult = userManager.AddToRole(user.Id, "Member");

                    if (identityResult.Succeeded)
                    {
                        //var userRoleResult = await userManager.AddToRoleAsync(user.Id, "Member");

                        //if (userRoleResult.Succeeded)
                        //{
                        //    //await SignInAsync(user, isPersistent: false);
                        //    //return RedirectToAction("Index", "Home");
                        //    //return Json(new { status = "success" });
                        //    return Ok();
                        //}

                        //return InternalServerError();
                        return Ok();
                    }
                    else
                    {
                        foreach (var error in identityResult.Errors)
                        {
                            ModelState.AddModelError(error,error);
                        }

                        return BadRequest(ModelState);
                    }
                }
                catch (Exception ex)
                {

                    throw ex;
                }
{'fraction_non_alphanumeric': 0.07198477245198132, 'fraction_numerical': 0.0, 'mean_word_length': 1.3815409971157808, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6819117', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1221, 'n_words': 269}