ASP.NET WebForm Identity使用

環境

  win10企業版x64+visual studio 2017+.net 4.5

step1

  基本使用+郵件確認+密碼重置

  https://docs.microsoft.com/en-us/aspnet/web-forms/overview/security/create-a-secure-aspnet-web-forms-app-with-user-registration-email-confirmation-and-password-reset

  問題

    文檔上郵件發送的包無法安裝成功,自己寫一個smtp發送就行

step2

  完善用戶表信息(增加修改字段)

  https://channel9.msdn.com/Events/dotnetConf/2014/ASP-NET-Identity-Security

  

 public class ApplicationUser : IdentityUser
    {
        public string HomeTown { get; set; }  //這個增加字段
        public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
        {
            // 請注意,authenticationType 必須與 CookieAuthenticationOptions.AuthenticationType 中定義的相應項匹配
            var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
            // 在此處添加自定義用戶聲明
            return userIdentity;
        }

        public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            return Task.FromResult(GenerateUserIdentity(manager));
        }

    }

  程序包管理控制檯執行下面的命令

    Enable-Migrations
    Add-Migration hometowm
    Update-database

step3

  facebook登陸

  https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

  注意

    要開啓站點的ssl

  問題1

    facebook的api頁面有更新,與文檔中的不一致,不過因爲之前做過fb js登陸,所以用起來沒什麼問題

  問題2

    fb的回掉裏面一直有deny,不能成功

    解決:更新nuget的facebook驗證相關的包

step4

  角色管理及權限控制

  http://www.cnblogs.com/chonghanyu/category/631169.html

  權限控制配置參考資料

  http://www.cftea.com/c/2011/01/LMZ3SKTUX6BL70T5.asp

  

<?xml version="1.0"?>
<configuration>

  <location path="Manage.aspx">
    <system.web>
      <authorization>
        <!--拒絕匿名用戶-->
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>

  <location path="RolesAdmin.aspx">
    <system.web>
      <authorization>
        <!--只允許admin角色訪問:注意大小寫-->
        <allow roles ="Admin"/>
        <!--拒絕所有用戶-->
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
</configuration>

  

  注意

    配置文件中的roles="Admin"的大小寫要與角色一致

    

代碼下載

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章