我需要帮助自定义mvc登录页面和仪表板显示用户详细信息和图片MVC4

时间:2015-11-07 17:55:12

标签: c# model-view-controller razor

我现在是一名学生,我正在学习MVC4。我在MVC中设计了一个自定义网站,并有一个自定义用户登录表单和仪表板。

我创建了一个登录文件,其中包含电子邮件和密码以及重新编号我复选框

之后我有如下代码

  1. 型号代码

    public class Login
                {
                    [Required(ErrorMessage = "Please Enter E-mail.")]
                    [DataType(DataType.EmailAddress)]
                    [StringLength(150)]               
                public string Email { get; set; }
                  [Required(ErrorMessage = "Please Enter Password.")]
                  [DataType(DataType.Password)]
                  [StringLength(30, MinimumLength = 6)]
                  public string Password { get; set; }
                }
    
  2. 用户控制器

    [HttpGet] public ActionResult Login()
    {
        return View();
    }
    #region Login Model
    [HttpPost]
    public ActionResult Login(Models.MessageGroup.Login User)
    {
        if (ModelState.IsValid)
        {
            if (IsValid(User.Email, User.Password))
            {  
                FormsAuthentication.SetAuthCookie(User.Email, false);
                return RedirectToAction("Dashboard", "User");
            }
            else
            {
                ModelState.AddModelError("error", "Please check Login details are incorrect.");
            }
        }
        return View(User);
    }
    
  3. 但如何在仪表板中显示用户的用户名和照片

    in

     <img src="" alt="">
     <span class="username">User Full name</span>
    

    如果我检查@User.Identity.Name即将出现的空白

    这是我的web.confg文件

    <?xml version="1.0"?>
    
    <configuration>
      <configSections>
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
          <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
      </configSections>
    
      <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
          </namespaces>
        </pages>
      </system.web.webPages.razor>
    
      <appSettings>
        <add key="webpages:Enabled" value="false" />
      </appSettings>
    
      <system.web>        
        <httpHandlers>
          <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
        </httpHandlers>
        <pages
            validateRequest="false"
            pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <controls>
            <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
          </controls>
        </pages>    
      </system.web>
    
      <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
          <remove name="BlockViewHandler"/>
          <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>   
      </system.webServer>  
    </configuration>
    

2 个答案:

答案 0 :(得分:0)

在web.config中使用此代码

<authentication mode="Forms"> 
<forms loginUrl="Login.cshtml" protection="All" timeout="30" name=".ASPXAUTH" 
 path="/" requireSSL="false" slidingExpiration="true" defaultUrl="Index.cshtml"  
 cookieless="UseDeviceProfile" enableCrossAppRedirects="false" /> </authentication> 

它将返回@User.Identity.Name

答案 1 :(得分:0)

如果您已经启用了Identity,那么web.config中的某些配置可能是这样的:

<system.webServer>
    <modules>
        <remove name="FormsAuthenticationModule" />
    </modules>
</system.webServer>

你必须重新开始行