मैंने पहले से ही एक एमवीसी वेबएपीआई परियोजना बनाई है और अब मैं प्रमाणीकरण और प्रमाणीकरण का उपयोग करना चाहता हूं। मुझे लगता है कि मैंने पहले से ही इस सुरक्षा को लागू किया है, लेकिन किसी कारण से कुछ बुरा हो जाता है, जब मैं अपने प्रमाण पत्र लिखता हूं और मैं कुछ वेब एपीआई तरीकों को कॉल करने का प्रयास करता हूं, तो संदेश "इस अनुरोध के लिए प्राधिकरण से इनकार कर दिया गया है" दिखाया गया है।प्राधिकरण अस्वीकार कर दिया गया है। हमेशा
यह वह कोड है जिसे मैंने कार्यान्वित किया था।
WebApiConfig:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Filters.Add(new AuthorizeAttribute());
}
रूटिंग कॉन्फ़िग:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Routing", action = "LogIn", id = UrlParameter.Optional }
);
}
नियंत्रक:
public class RoutingController : Controller
{
//
// GET: /Routing/
public ActionResult Index()
{
return View();
}
public ActionResult Projects()
{
return View();
}
public ActionResult Users()
{
return View();
}
public ActionResult LogIn()
{
return View();
}
[HttpPost]
public JsonResult LogInPost(string userName, string password)
{
User user = new User();
RoleByUser rByU = new RoleByUser();
password = UserController.EncriptPassword(password);
string url = string.Empty;
var checkUser = user.Get(userName);
var userExists = (from userInList in checkUser where userInList.UserName == userName && userInList.Password == password select userInList).FirstOrDefault();
if(userExists!= null)
{
var roles = (from roleByUser in userExists.listOfRole select roleByUser.RoleName.Trim()).ToArray();
IPrincipal principal = new GenericPrincipal(
new GenericIdentity(userExists.UserName), roles);
SetPrincipal(principal);
url = "Routing/Users";
}
return Json(url);
}
private void SetPrincipal(IPrincipal principal)
{
Thread.CurrentPrincipal = principal;
if (System.Web.HttpContext.Current != null)
{
System.Web.HttpContext.Current.User = principal;
}
}
}
HTML:
<link href="~/css/Style.css" rel="stylesheet" type="text/css" />
<div class="container">
<div class="card card-container">
<img id="STK" class="profile-img-card" src="Images/Softtek.png" />
<p id="profile-name" class="profile-name-card"></p>
<form class="form-signin">
<span id="reauth-email" class="reauth-email"></span>
<input type="text" id="txtUserName" class="form-control" placeholder="Email address" required autofocus />
<input type="password" id="txtPassword" class="form-control" placeholder="Password" required />
<div id="remember" class="checkbox">
<label>
<input type="checkbox" value="remember-me" /> Remember me
</label>
</div>
@*<button id="btnLogIn" class="btn btn-lg btn-primary btn-block btn-signin" >Sing In</button>*@
</form><!-- /form -->
<button id="btnLogIn" class="btn btn-lg btn-primary">Sing In</button>
<a href="#" class="forgot-password">
Forgot the password?
</a>
</div><!-- /card-container -->
</div><!-- /container -->
जे एस:।
$ (document) .ready (function() { $ ('# btnLogIn') पर क्लिक करें (प्रवेश); });
function logIn() {
$.ajax({
type: "POST",
url: "http://localhost:21294/Routing/LogInPost",
dataType: "json",
data: { userName: $('#txtUserName').val(), password: $('#txtPassword').val() },
success: function (data) {
if(data!= "" && data!= undefined && data!= null)
window.location.href = data;
},
error: function (err, e, error) {
toastr.error('Error')
}
});
मुझे लगता है कि समस्या यह है कि मैं थ्रेड को लागू कर रहा हूं। वर्तमान प्रिंसिपल और Current.User, क्योंकि अगर मैं अपने एपीआई नियंत्रक में विशेषता [AllowAnonymous] जोड़ता हूं तो ऐप ठीक काम करता है, लेकिन अगर मैं विशेषता जोड़ता हूं [प्राधिकरण (भूमिकाएं = रोलइन्टिटी .Collaborator)] उदाहरण के लिए आवेदन इस भूमिका को पहचान नहीं है। इसलिए मुझे लगता है कि मुझे दूसरी जगह या किसी अन्य तरीके से वर्तमान प्रिंसिपल को लागू करना चाहिए, क्या आपको पता है कि मुझे कहां इम्प्लांट करना चाहिए? – user3442776
यह देर से प्रतिक्रिया है, लेकिन यदि उपयोगकर्ता ने अभी तक प्रमाणीकृत नहीं किया है तो आप भूमिका के लिए अधिकृत करने की कार्रवाई के लिए कैसे उम्मीद कर सकते हैं? –