<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-$route-service-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular-route.js"></script>
<script>
var app = angular.module('ngRouteExample', ['ngRoute']);
app.factory('rootingfact', function ($http, $q) {
return {
getPerson: function (entity) {
var deferred = $q.defer();
$http.get('/api/Home/getvalue', {
// params: { op: 'get' },
headers: { "AUTH_TOKEN": entity.token }
}).success(function (data) {
deferred.resolve({
title: data.title,
cost: data.price
});
}).error(function (msg, code) {
deferred.reject(msg);
$log.error(msg, code);
});
return deferred.promise;
}
}
});
app.controller('RootControllerCreate', function ($scope, $http, rootingfact) {
rootingfact.getPerson($scope.entity).then(
function (result) {
alert('authorized');
},
function (error) {
alert('error');
}
);
});
app.controller('RootControllerDetail', function ($scope) {
});
app.controller('RootControllerUpdate', function ($scope) {
});
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/Routing/Detail', {
templateUrl: 'Routing/Detail',
controller: 'RootControllerDetail'
})
.when('/Routing/Create', {
templateUrl: 'Routing/Create',
controller: 'RootControllerCreate'
})
.when('/Routing/Update', {
templateUrl: 'Routing/Update',
controller: 'RootControllerUpdate'
})
.otherwise({
redirectTo: '/'
});
// configure html5 to get links working on jsfiddle
// $locationProvider.html5Mode(true);
});
app.run(function ($rootScope, $location) {
$rootScope.$on("$routeChangeStart", function (args) {
var path = $location.path().split('/');
$rootScope.entity = {};
/* $rootScope.entity.ctrl = path[1];
$rootScope.entity.action = path[2];*/
$rootScope.entity.token = "124_sdfssdfsdfsdf_" + path[1] + "_" + path[2];
})
//$rootScope.$on("$routeChangeSuccess"....
//$rootScope.$on("$routeChangeError"....
});
</script>
</head>
<body ng-app="ngRouteExample">
Choose:
<a href="/#/Routing/Create">Create</a> |
<a href="/#/Routing/Detail">Detail</a> |
<a href="/#/Routing/Update">Update</a> |
<div ng-view></div>
</body>
</html>
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
void Application_BeginRequest(object sender, EventArgs e)
{
var context = HttpContext.Current;
var request = context.Request;
string url = request.Url.LocalPath;
if (url.IndexOf("api/") != -1)
{
IEnumerable<string> headerValues = request.Headers.GetValues("AUTH_TOKEN");
if (headerValues != null)
{
string[] token = headerValues.ToArray();
string[] all = token[0].Split('_');
// var dbId = dbContext.abc.FirstOrDefault(x => x.token == token);
if ("sdfssdfsdfsdf" != all[1])
{
throw new Exception("User now exists");
}
}
else
{
throw new Exception("Error");
}
}
}
}
public class test
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string name { get; set; }
public decimal? allowance { get; set; }
public bool paid { get; set; }
}
public class abc
{
public int id { get; set; }
public string token { get; set; }
}
public class context : DbContext
{
public DbSet<abc> abc { get; set; }
}
public class HomeController : ApiController
{
[HttpGet]
public HttpResponseMessage getvalue()
{
string abc = "";
return new HttpResponseMessage
{
Content = new StringContent(abc,
System.Text.Encoding.UTF8, "application/json")
};
}
}
Sunday, August 2, 2015
Angular Js Server Side And Client Side Authentication for web api
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment