如何删除默认的ASP.NET Core Identity终结点?

时间:2018-11-30 14:51:19

标签: c# asp.net-core asp.net-core-mvc asp.net-identity

假设我要编写自己的Login,Logout端点及其视图

但是由于某些原因,我正在努力删除现有的端点

每当我删除可能与这些终结点关联的内容时,它们就会重新创建自己并返回其默认视图。

基本上我想删除从ASP.NET Core Identity生成的端点的尽可能多的默认端点/视图

关于我该如何实现的任何想法?

        "Templates/Identity/Pages/Account/Account.AccessDenied.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.AccessDenied.cshtml",
        "Templates/Identity/Pages/Account/Account.ConfirmEmail.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.ConfirmEmail.cshtml",
        "Templates/Identity/Pages/Account/Account.ExternalLogin.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.ExternalLogin.cshtml",
        "Templates/Identity/Pages/Account/Account.ForgotPassword.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.ForgotPassword.cshtml",
        "Templates/Identity/Pages/Account/Account.ForgotPasswordConfirmation.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.ForgotPasswordConfirmation.cshtml",
        "Templates/Identity/Pages/Account/Account.Lockout.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.Lockout.cshtml",
        "Templates/Identity/Pages/Account/Account.Login.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.Login.cshtml",
        "Templates/Identity/Pages/Account/Account.LoginWith2fa.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.LoginWith2fa.cshtml",
        "Templates/Identity/Pages/Account/Account.LoginWithRecoveryCode.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.LoginWithRecoveryCode.cshtml",
        "Templates/Identity/Pages/Account/Account.Logout.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.Logout.cshtml",
        "Templates/Identity/Pages/Account/Account.Register.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.Register.cshtml",
        "Templates/Identity/Pages/Account/Account.ResetPassword.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.ResetPassword.cshtml",
        "Templates/Identity/Pages/Account/Account.ResetPasswordConfirmation.cs.cshtml",
        "Templates/Identity/Pages/Account/Account.ResetPasswordConfirmation.cshtml",
        "Templates/Identity/Pages/Account/Account._ViewImports.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.ChangePassword.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.ChangePassword.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.DeletePersonalData.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.DeletePersonalData.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.Disable2fa.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.Disable2fa.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.DownloadPersonalData.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.DownloadPersonalData.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.EnableAuthenticator.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.EnableAuthenticator.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.ExternalLogins.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.ExternalLogins.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.GenerateRecoveryCodes.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.GenerateRecoveryCodes.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.Index.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.Index.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.ManageNavPages.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.PersonalData.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.PersonalData.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.ResetAuthenticator.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.ResetAuthenticator.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.SetPassword.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.SetPassword.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.ShowRecoveryCodes.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.ShowRecoveryCodes.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.TwoFactorAuthentication.cs.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage.TwoFactorAuthentication.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage._Layout.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage._ManageNav.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage._StatusMessage.cshtml",
        "Templates/Identity/Pages/Account/Manage/Account.Manage._ViewImports.cshtml",
        (...)

2 个答案:

答案 0 :(得分:5)

您不能使用AddDefaultIdentity,因为在内部会调用AddDefaultUI,其中包含您不需要的Razor Pages“端点”。您需要改用AddIdentity<TUser, TRole>AddIdentityCore<TUser>

答案 1 :(得分:0)

我在ASPNET的Github上检查了源代码并对其进行了一点编辑

https://github.com/aspnet/Identity/blob/c7276ce2f76312ddd7fccad6e399da96b9f6fae1/src/UI/IdentityServiceCollectionUIExtensions.cs#L47

try {
                    //getting the whole json object from the response
                    JSONObject obj = new JSONObject(response);

                    JSONArray jArray = obj.getJSONArray("squad");

                    for (int i = 0; i < jArray.length(); i++) {

                        JSONObject jsonObject = jArray.getJSONObject(i);


                        JSONArray bArray = jsonObject.getJSONArray("players");
                        for (int j = 0; j < bArray.length(); j++){


                        JSONObject jsonObject1 = bArray.getJSONObject(j);
                        cricket_Player_POJO cricketPlayer = new cricket_Player_POJO(jsonObject1.getString("name"));

                        cricketListItem.add(cricketPlayer);

                        }

                     }

                    cricket_Player_List cricketList = new cricket_Player_List(cricketListItem, getApplicationContext());
                    cricketPLayerlistView.setAdapter(cricketList);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

代码如下:

扩展方法:

public static IdentityBuilder AddDefaultIdentity<TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions) where TUser : class
{
    services.AddAuthentication(o =>
    {
        o.DefaultScheme = IdentityConstants.ApplicationScheme;
        o.DefaultSignInScheme = IdentityConstants.ExternalScheme;
    })
    .AddIdentityCookies(o => { });

    return services.AddIdentityCore<TUser>(o =>
    {
        o.Stores.MaxLengthForKeys = 128;
        configureOptions?.Invoke(o);
    })
    .AddDefaultUI() // It'll be removed
    .AddDefaultTokenProviders();
}

Startup.cs

public static class IServiceCollectionExtensions
{
    public static IdentityBuilder AddCustomDefaultIdentity<TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions) where TUser : class
    {
        services.AddAuthentication(o =>
        {
            o.DefaultScheme = IdentityConstants.ApplicationScheme;
            o.DefaultSignInScheme = IdentityConstants.ExternalScheme;
        })
        .AddIdentityCookies(o => { });

        return services.AddIdentityCore<TUser>(o =>
        {
            o.Stores.MaxLengthForKeys = 128;
            configureOptions?.Invoke(o);
        })
        .AddDefaultTokenProviders();
    }
}