Beruflich Dokumente
Kultur Dokumente
Step - 1 : Create New Project. Go to File > New > Project > Select asp.net MVC4 web
application > Entry Application Name > Click OK > Select Basic Application > Select
view engine Razor > OK
Step-3: Add new action into your controller for anonymous user
Here I have used "Index" Action. Please write this following code
1.
4. {
5. return View();
6. }
7.
2. @{
3. ViewBag.Title = "Index";
4. }
5.
6. <h2>Index</h2>
7.
9.
1.
4. {
5. return View();
6. }
7.
2. @{
3. ViewBag.Title = "MyProfile";
4. }
5.
6. <h2>MyProfile</h2>
7.
8. <h3>Welcome @(Request.IsAuthenticated ?
HttpContext.Current.User.Identity.Name : "Guest") - This is for
Authorized user </h3>
9.
Optional: Here I have added Bootstrap css in the layout page for Responsive design.
1.
2. using System.ComponentModel.DataAnnotations;
3.
4. namespace MvcAuthentication.Models
5. {
7. {
8. [Required(ErrorMessage="Username
required.",AllowEmptyStrings=false)]
10.
12.
[DataType( System.ComponentModel.DataAnnotations.DataType.Passwor
d)]
15. }
16. }
17.
1.
3. {
4. return View();
5. }
6.
1.
2. @model MvcAuthentication.Models.Login
3.
4. @{
5. ViewBag.Title = "Login";
6. }
7.
8. <h2>Login</h2>
9.
11. @Html.ValidationSummary(true)
12. @Html.AntiForgeryToken()
13. <fieldset>
14. <legend>Login</legend>
15.
18. </div>
22. </div>
23.
26. </div>
30. </div>
31.
34. </div>
38. </div>
39.
40. <p>
42. </p>
43. </fieldset>
44. }
45.
46. <div>
48. </div>
49.
51. @Scripts.Render("~/bundles/jqueryval")
52. }
53.
54.
1.
2. <authentication mode="Forms">
4. </authentication>
5.
RUN APP HERE FOR TEST IS ALL WORKING AS EXPECTED OR
NOT
1.
2. [HttpPost]
3. [ValidateAntiForgeryToken]
5. {
7. {
9. if (user != null)
10. {
11. FormsAuthentication.SetAuthCookie(user.Username,
l.RememberMe);
12. if (Url.IsLocalUrl(ReturnUrl))
13. {
15. }
16. else
17. {
19. }
20. }
21. }
22. ModelState.Remove("Password");
25.
1.
2. [Authorize]
4. {
5. FormsAuthentication.SignOut();
7. }
8.
1. <li>
2. @{
3. if (Request.IsAuthenticated)
4. {
5. using (Html.BeginForm("Logout","MyAccount",
FormMethod.Post,new{ id = "logoutForm"}))
6. {
7. <a
href="javascript:document.getElementById('logoutForm').submit()">
Logout</a>
8. }
9. }
10. else
11. {
12.
@Html.ActionLink("Login","Login","MyAccount")
13. }
14. }
15. </li>