// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { string temp = SingleTon.readSetting("secret"); SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(temp)); services.AddAuthentication("OAuth").AddJwtBearer("OAuth", config => { config.TokenValidationParameters = new TokenValidationParameters() { ValidIssuer = SingleTon.readSetting("issuer"), ValidAudience = SingleTon.readSetting("audience"), IssuerSigningKey = key, }; }); services.AddControllers(); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder .AllowAnyMethod() .AllowAnyOrigin() .AllowAnyHeader()); }); }
public ActionResult <User> GetAuthToken(string username, string password) { User LoginData = new User() { username = username, password = password }; List <User> Users = SingleTon.GetSQLAccessor().GetUser(); foreach (User element in Users) { if (element.username == LoginData.username) { User CurrentUser = element; if (SingleTon.GetCryptoHashing().VerifyHash(LoginData.password, element.password)) { Claim[] claims = new[] { new Claim(JwtRegisteredClaimNames.Sub, element.userID) }; SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SingleTon.readSetting("secret"))); string Algo = SecurityAlgorithms.HmacSha256; SigningCredentials signingCredentials = new SigningCredentials(key, Algo); JwtSecurityToken token = new JwtSecurityToken(SingleTon.readSetting("issuer"), SingleTon.readSetting("audiance"), claims, notBefore: DateTime.Now, expires: DateTime.Now.AddDays(1), signingCredentials); CurrentUser.password = ""; CurrentUser.token = new JwtSecurityTokenHandler().WriteToken(token); return(Ok(CurrentUser)); } } } return(BadRequest()); }
private bool OpenConnection(out SqlConnection connection) { connection = null; try { connection = new SqlConnection(SingleTon.readSetting("DBConnectionString")); connection.Open(); return(true); } catch (Exception ex) { Console.WriteLine(ex); return(false); } }