Raju Fodse

Raju Fodse

  • 1.8k
  • 207
  • 13.4k

Role base Authentication in MVC

Feb 22 2020 4:12 AM
I am creating role base authentication with User and UserRole Table. But my login page redirect again and again. I can see 302 error in Inspect element utility.
 
Below are source code 
MyAccount Controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Security;  
  7. using EasyApp.Models;  
  8.   
  9. namespace EasyApp.Controllers  
  10. {  
  11.     public class MyAccountController : Controller  
  12.     {  
  13.         // GET: MyAccount  
  14.         [AllowAnonymous]  
  15.         public ActionResult Login()  
  16.         {  
  17.             return View();  
  18.         }  
  19.   
  20.         [AllowAnonymous]  
  21.         [HttpPost]  
  22.         [ValidateAntiForgeryToken]  
  23.           
  24.         public ActionResult Login(Login l, string ReturnUrl = "")  
  25.         {  
  26.             using (LPDBContext dc = new LPDBContext())  
  27.             {  
  28.                 var user = dc.Users.Where(a => a.Username.Equals(l.Username) && a.Password.Equals(l.Password)).FirstOrDefault();  
  29.                 if (user != null)  
  30.                 {  
  31.                     FormsAuthentication.SetAuthCookie(user.Username, false);  
  32.                     if (Url.IsLocalUrl(ReturnUrl))  
  33.                     {  
  34.                         return Redirect(ReturnUrl);  
  35.                     }  
  36.   
  37.                 }  
  38.                 ModelState.AddModelError("UserName""username or password is incorrect !");  
  39.                 ModelState.Remove("Password");  
  40.                 return View();  
  41.             }  
  42.   
  43.   
  44.         }  
  45.   
  46.   
  47.         [Authorize]  
  48.     public ActionResult Logout()  
  49.     {  
  50.         FormsAuthentication.SignOut();  
  51.         return RedirectToAction("Index""Main");  
  52.     }  
  53. }  
  54.     }  
Main Controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace EasyApp.Controllers  
  8. {  
  9.     [Authorize]  
  10.     public class MainController : Controller  
  11.     {  
  12.         // GET: Main  
  13.         [AllowAnonymous]  
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.         [Authorize]  
  19.         public ActionResult EDPDashboard()  
  20.         {  
  21.             return View();  
  22.         }  
  23.   
  24.         [Authorize(Roles ="Admin")]  
  25.         public ActionResult DPDashboard()  
  26.         {  
  27.             return View();  
  28.         }  
  29.   
  30.         [Authorize(Roles = "Admin")]  
  31.         public ActionResult RTFlim()  
  32.         {  
  33.             return View();  
  34.         }  
  35.     }  
  36. }  
Web.Config File
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.   For more information on how to configure your ASP.NET application, please visit  
  4.   http://go.microsoft.com/fwlink/?LinkId=301880  
  5.   -->  
  6. <configuration>  
  7.   <configSections>  
  8.     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->  
  9.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  
  10.   </configSections>  
  11.   <connectionStrings>  
  12.     <add name="LPDBContext" connectionString="metadata=res://*/Models.EasyAppDB.csdl|res://*/Models.EasyAppDB.ssdl|res://*/Models.EasyAppDB.msl;provider=System.Data.SqlClient;provider connection string="data source=EDP\SQLEXPRESS;initial catalog=LPDB;persist security info=True;user id=sa;password=sa123;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />  
  13.   </connectionStrings>  
  14.   <appSettings>  
  15.     <!--<add key="owin:appStartup" value="MyAccount.Login" />-->  
  16.     <!--<add key="owin:AutomaticAppStartup" value="false" />-->  
  17.     <add key="webpages:Version" value="3.0.0.0" />  
  18.     <add key="webpages:Enabled" value="false" />  
  19.     <add key="ClientValidationEnabled" value="true" />  
  20.     <add key="UnobtrusiveJavaScriptEnabled" value="true" />  
  21.   </appSettings>  
  22.   <system.web>  
  23.     <globalization uiCulture="en" culture="en-GB" />  
  24.     <authentication mode="Forms">  
  25.       <forms loginUrl="MyAccount/Login"/>  
  26.     </authentication>  
  27.     <roleManager defaultProvider="myroleprovider" enabled="true">  
  28.       <providers>  
  29.         <clear/>  
  30.         <add name="myroleprovider" type="EasyApp.WebRoleProvider"/>  
  31.       </providers>  
  32.     </roleManager>  
  33.     <compilation debug="true" targetFramework="4.5" />  
  34.     <httpRuntime targetFramework="4.5" />  
  35.     <pages>  
  36.       <namespaces>  
  37.         <add namespace="GridMvc" />  
  38.         <add namespace="System.Web.Helpers" />  
  39.         <add namespace="System.Web.Mvc" />  
  40.         <add namespace="System.Web.Mvc.Ajax" />  
  41.         <add namespace="System.Web.Mvc.Html" />  
  42.         <add namespace="System.Web.Optimization" />  
  43.         <add namespace="System.Web.Routing" />  
  44.         <add namespace="System.Web.WebPages" />  
  45.       </namespaces>  
  46.     </pages>  
  47.   </system.web>  
  48.   <system.webServer>  
  49.     <modules>  
  50.       <!--<remove name="FormsAuthentication" />-->  
  51.     </modules>  
  52.     <directoryBrowse enabled="true" />  
  53.   </system.webServer>  
  54.   <runtime>  
  55.     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
  56.       <dependentAssembly>  
  57.         <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />  
  58.         <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />  
  59.       </dependentAssembly>  
  60.       <dependentAssembly>  
  61.         <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />  
  62.         <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />  
  63.       </dependentAssembly>  
  64.       <dependentAssembly>  
  65.         <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />  
  66.         <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />  
  67.       </dependentAssembly>  
  68.       <dependentAssembly>  
  69.         <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />  
  70.         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />  
  71.       </dependentAssembly>  
  72.       <dependentAssembly>  
  73.         <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />  
  74.         <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />  
  75.       </dependentAssembly>  
  76.       <dependentAssembly>  
  77.         <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />  
  78.         <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />  
  79.       </dependentAssembly>  
  80.       <dependentAssembly>  
  81.         <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />  
  82.         <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />  
  83.       </dependentAssembly>  
  84.     </assemblyBinding>  
  85.   </runtime>  
  86.   <entityFramework>  
  87.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">  
  88.       <parameters>  
  89.         <parameter value="mssqllocaldb" />  
  90.       </parameters>  
  91.     </defaultConnectionFactory>  
  92.     <providers>  
  93.       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />  
  94.     </providers>  
  95.   </entityFramework>  
  96.   <system.codedom>  
  97.     <compilers>  
  98.       <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />  
  99.       <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />  
  100.     </compilers>  
  101.   </system.codedom>  
  102. </configuration>  
 and My Error
  User base authentication works but Role base could not works. whats is the reason.....?
 

Answers (6)